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 - ReferenceError: Can't find variable when using a declared enum #160

Closed
splintor opened this issue Jun 18, 2015 · 7 comments
Closed

Comments

@splintor
Copy link

Take a look at the following TypeScript code:

/// <reference path='../../../../typings/karma-jasmine/karma-jasmine.d.ts' />
declare module MyModule{
    export enum MyEnum{
        First = 1,
        Second = 2,
        Third = 3,
    }
}

describe("wallaby test", () => {
    it("should test TypeScript enums", () => {
        var a = MyModule.MyEnum.First;
        expect(a).toBe(1);
    });
});

Wallaby show an error on this code which says:
ReferenceError: Can't find variable: MyModule

This seems to be a problem with the declare module statement, as replacing it with module (with no declare) makes the problem go away, but this is the way the enum is declared in the d.ts file we are using.

@ArtemGovorov
Copy link
Member

I may be missing something but TypeScript declarations are not generated into JavaScript, so the JavaScript for your example looks like this and MyModule is not there, so the same error would happen.

Is it working for you without wallaby? If so, would it be possible to create a working sample, perhaps a sample Github repository so I could have a look?

@splintor
Copy link
Author

There seems to be a difference between the online compiler (and the npm typescript version 1.5.0-beta compiler) and my tsc.exe compiler (version 1.0.3.0).

See this simple code:

declare module MyModule{
    export enum MyEnum{
        First = 1,
        Second= 2,
        Third = 3,
    }
}

function test() {
    return MyModule.MyEnum.First;
}

The online compiler (and npm typescript compiler) transpiles it to:

function test() {
    return MyModule.MyEnum.First;
}

But when I run it with tsc, I get this js file generated:

function test() {
    return 1 /* First */;
}

What workaround can I use, provided that I can't really change my d.ts file?

@ArtemGovorov
Copy link
Member

I'll have a look why it happens, in a meantime, as a workaround, you may use bootstrap function where you may define the enum like:

bootstrap: function (wallaby) {
  MyModule = MyModule || {};
  MyModule.MyEnum = MyModule.MyEnum || {};
  MyModule.MyEnum.First = 1;
  MyModule.MyEnum.Second = 2;
  // etc
}

@ArtemGovorov
Copy link
Member

It is indeed the difference between typescript compilers.

v1.4 replaces MyModule.MyEnum.First with 1 and v1.5 beta doesn't. Wallaby.js is using the latest, so that's why it happens.

@ArtemGovorov
Copy link
Member

Created the issue in TypeScript repo.

@splintor
Copy link
Author

Thanks.
It seems using export const enum works OK. I'll try to see if I can change my d.ts file to use const enum.

@ArtemGovorov
Copy link
Member

So as it looks like const enum is the recommended way and it works, closing the issue.

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

No branches or pull requests

2 participants