Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript compilation: declare const is not de-referenced properly #227

Closed
iamakulov opened this issue Jul 5, 2020 · 2 comments
Closed

Comments

@iamakulov
Copy link

Hey,

I stumbled upon a ESBuild bug with the following piece of code (simplified from a real project):

export namespace Assets {
    export declare const service: string

    export function on(channel: string) {
        return someCall(service, channel) // ← A
    }
}

// Later: assign the actual `service` implementation
(Assets as any).service = getTheServiceFromSomewhere()

ESBuild seems to leave the service reference at line A as-is. This results in Uncaught ReferenceError: service is not defined.

TypeScript output (playground):

export var Assets;
(function (Assets) {
    function on(channel) {
        // ↓ Note how `service` is de-referenced to `Assets.service`
        return someCall(Assets.service, channel);
    }
    Assets.on = on;
})(Assets || (Assets = {}));
// Later: assign the actual `service` implementation
Assets.service = getTheServiceFromSomewhere();

ESBuild output:

// ...
var Assets;
(function(Assets2) {
    function on(channel) {
    // ↓ Note how `service` is kept as-is
    return someCall(service, channel);
    }
    Assets2.on = on;
})(Assets || (Assets = {}));
Assets.service = getTheServiceFromSomewhere();

Compiled with:

esbuild --bundle test.ts --outfile=out.js
@evanw
Copy link
Owner

evanw commented Jul 6, 2020

Interesting. Thanks for reporting this. This will be good to fix.

@evanw evanw closed this as completed in dd9fc67 Jul 6, 2020
@evanw
Copy link
Owner

evanw commented Jul 6, 2020

This should be fixed in version 0.5.23.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants