`keepClassNames` 会打破类装饰器
作者: falkenhawk创建于 2022年11月26日更新于 2026年9月14日
标签C-bug
Describe the bug Hi, thanks for this great library and providing continuous support for it. I have experienced an issue which I have been debugging for days. jsc.keepClassNames breaks references, when class is decorated. For example, creating new instance of a class, calling static method, uses the non-decorated class instead of the decorated one. Here I try to provide the most concise and minimal reproduction I was able to come up with. ### Input code TypeScript function CommonProp() { return <T extends { new (...args: any[]): {}; }>(Base: T) => { return class extends Base { public prop = 100; } }; } @CommonProp() class ClassX { public static checkX() { console.log(String(ClassX)); console.log(new ClassX()); // @ts-ignore console.log('class prop:', new ClassX().prop); } } ClassX.checkX(); } ### Config json { "jsc": { "parser": { "syntax": "TypeScript", "decorators": true }, "target": "es2020", "keepClassNames": true } } ### Playground link https://play.swc.rs/?version=1.3.19&code=H4sIAAAAAAAA2WPwWrDMBBE7wb%2Fw9wiQaukV7sJofmBQnMIlB5UVXFNnZWRZNpi9O%2BNurEJ5LILmtmnmeNAJraOsHOnk6Nn73ohMZYF4G0cPOFxD%2FsTLX0EjCD7DaGU0r4JFTT9vr7JCmOqkTbiSQdbYS%2Bx3jBhZphOhzBjsm8yAP3w3rUG%2FflnrPGwWtWspP%2BdZyqLstheBywLJu7yPDDrwglRx%2FMyn9Z8HaYqgHEUXGdV5xrxEn1LjeBjKetbR67Jspj15RLbGO7bhpy3tycLTpRrVIs7XBNUfmRMurRhSU0p6z%2Fl5TWZhwEAAA%3D%3D&config=H4sIAAAAAAAAAzXKOwqAMBAFwLu82kIs09p7hyU%2BxG%2FC7gqG4N3Vwm6KqVgsIlRkUaN%2BsnK4XAjwkmlR5%2BxoMDImFU9qCK4n7wYuOtHfSOvarn3TSuZ%2BE7NBdv7xfgBpgK6LZAAAAA%3D%3D ### Expected behavior Class decorators + keepClassNames should work properly in compiled code. Works in tsc: bash ❯ tsc ❯ node index.js class extends Base { constructor() { super(...arguments); this.prop = 100; } } ClassX { prop: 100 } class prop: 100 ### Actual behavior bash ❯ npx swc index.ts -o index.js Successfully compiled 1 file with swc. ❯ node index.js class ClassX { static checkX() { console.log(String(ClassX)); console.log(new ClassX()); // @ts-ignore console.log('class prop:', new ClassX().prop); } } ClassX {} class prop: undefined ### Version 1.3.19 ### Additional context This issue affects all devs using ts-node + swc, with target >=es2015, and also @swc-node/register, as they do not provide a way to disable keepClassNames (they ignore .swcrc file and instead transform tsconfig.json to a set of options passed directly to swc, together with swcrc: false) But still, if there was an option to disable keepClassNames, it could then break i.a. TypeORM which requires it to be enabled. (related: https://GitHub.com/TypeStrong/ts-node/issues/1343) More tests: Works when keepClassNames: false but of course original class name is then altered. bash ❯ npx swc index.ts -o index2.js Successfully compiled 1 file with swc. ❯ node
内容来源: swc-project/swc