TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

`"importHelpers": true` combined with `"module": "preserve"` results in undefined helper usage

Open clydin opened this issue 1 year ago • 0 comments

🔎 Search Terms

importHelpers, module, preserve

🕗 Version & Regression Information

Fails with TS2354 in versions 5.4.x. Generates invalid code in >= 5.5 including nightly.

⏯ Playground Link

https://www.typescriptlang.org/play/?esModuleInterop=false&declaration=false&importHelpers=true&target=9&jsx=0&module=200#code/FAMwrgdgxgLglgewgAgKIA8CGBbADgGwFMARQqBAJ0xkoAoBKALmUwgE9kBvAX2GAAEMOAiTKVqdesELpclGMij5MAZxXIAglz7BuQA

💻 Code


function ExampleDecorator(): any {}

@ExampleDecorator()
export class A {}

🙁 Actual behavior

import * as tslib_1 from "tslib";
function ExampleDecorator() { }
let A = (() => {
    let _classDecorators = [ExampleDecorator()];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    var A = class {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
            __esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            A = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
            __runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return A = _classThis;
})();
export { A };

🙂 Expected behavior

import * as tslib_1 from "tslib";
function ExampleDecorator() { }
let A = (() => {
    let _classDecorators = [ExampleDecorator()];
    let _classDescriptor;
    let _classExtraInitializers = [];
    let _classThis;
    var A = class {
        static { _classThis = this; }
        static {
            const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
            tslib_1.__esDecorate(null, _classDescriptor = { value: _classThis }, _classDecorators, { kind: "class", name: _classThis.name, metadata: _metadata }, null, _classExtraInitializers);
            A = _classThis = _classDescriptor.value;
            if (_metadata) Object.defineProperty(_classThis, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
            tslib_1.__runInitializers(_classThis, _classExtraInitializers);
        }
    };
    return A = _classThis;
})();
export { A };

Additional information about the issue

The namespace import identifier for tslib appears to not be used when generating the helper calls.

The baseline reference also appears to demonstrate the issue: https://github.com/microsoft/TypeScript/blob/497316f7e847bc22a009244ba80a45c3bc6fe6b6/tests/baselines/reference/importHelpersVerbatimModuleSyntax.js#L48-L53

clydin avatar Aug 28 '24 20:08 clydin