Microsoft/TypeScript

TSC emits invalid .d.ts file with reserved keywords in identifier position

Open

#53,111 创建于 2023年3月6日

在 GitHub 查看
 (4 评论) (2 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: Declaration EmitHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

🔎 Search Terms

delete reserved keyword .d.ts .d.ts reserved keyword

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about invalid .d.ts types

⏯ Playground Link

Playground link with relevant code

💻 Code

const obj1 = {
    delete: "foobar"
};

const obj2 = {
  x: obj1.delete
};

export default obj2;

🙁 Actual behavior

The emitted .d.ts file contains delete in an identifier position. This in turns triggers an error when consumed: Identifier expected. 'delete' is a reserved word that cannot be used here.

Generated .d.ts:

export default obj2;
declare namespace obj2 {
    import x = obj1.delete;
    export { x };
}
declare namespace obj1 {
    const _delete: string;
    export { _delete as delete };
}

🙂 Expected behavior

The generated .d.ts file should be valid.


Further notes

  • Invalid .d.ts files are generated both if the input is a .js or .ts file
  • I did not find any combination of flags/options/versions that would generate a valid .d.ts file.
  • I did find a workaround by altering the source file 👇

Workaround

This issue can currently be worked around by using ["delete"] instead of .delete. E.g. this:

const obj1 = {
    delete: "foobar"
};

const obj2 = {
  x: obj1["delete"]
};

export default obj2;

Generates the following valid .d.ts:

export default obj2;
declare namespace obj2 {
    const x: string;
}

贡献者指南