Microsoft/TypeScript
在 GitHub 查看static block on unnamed class produces invalid javascript
Open
#60,879 创建于 2024年12月30日
BugDomain: JS EmitHelp Wanted
仓库指标
- Star
- (48,455 star)
- PR 合并指标
- (平均合并 6天 17小时) (30 天内合并 9 个 PR)
描述
🔎 Search Terms
- class
- invalid
- codegen
- static
🕗 Version & Regression Information
- This changed after version 4.4
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about classes, static members of classes
- I was unable to test this on prior versions because static blocks where introduced in typescript 4.4
⏯ Playground Link
💻 Code
export default class{
static demo:number;
static {
this.demo = 1;
}
}
🙁 Actual behavior
TypeScript produces an invalid output where the code references an undefined default_1
I'm assuming the empty class is meant to be assigned to this.
example output:
var _a;
class {
}
_a = default_1;
(() => {
_a.demo = 1;
})();
export default default_1;
🙂 Expected behavior
I would expect typescript to assign the class to the generated default_1
Additional information about the issue
TypeScript has been exhibiting this behavior since support for static blocks was introduced as far as I can tell, prior to version 4.4.4 in the playground produces a different set of invalid code where it just moves the block outside of the class with no modification.
this still technically happens without the export default but typescript does have a warning.