typings icon indicating copy to clipboard operation
typings copied to clipboard

Enums with definition

Open originalmoose opened this issue 10 years ago • 6 comments

Description

I am having an issue with enums and definition files. I am using Typelite to create the definition and enum in question so this may end up being an issue that should be aimed at them. Below are the files generated by typelite.

Enums.ts

module Test {
    export const enum TestEnum {
        Value = 0,
        Test = 1
    }
}

index.d.ts

/// <reference path="Enums.ts" />
declare module Test {
    interface Test {
        value: TestEnum;
    }
}

I created the following file for typings to use when installing.

typings.json

{
  "name": "test-lib",
  "global": true,
  "files": [
    "index.d.ts",
    "Enums.ts"
  ],
  "dependencies": {
  }
}

When installed I get the following output

// Generated by typings
// Source: ../test/index.d.ts
declare module Test {
    interface Test {
        value: TestEnum;
    }
}

// Generated by typings
// Source: ../test/Enums.ts
module Test {
    export const enum TestEnum {
        Value = 0,
        Test = 1
    }
}

The problem is this will throw an error in the typescript compiler because the enums module doesn't have a declare. Currently after running an install I just have to make sure to add the declare, no big deal but it does get sort of annoying to have to do this each time. Once I add the declare, everything works as expected. I can reference my enums by doing Test.TestEnum.Value and as long as the js file generated from Enums.ts is included in the page everything works great.

Is there a better way to handle enums? or is there maybe a flag somewhere I can set to inform typings to append a declare to modules in the enums file?

originalmoose avatar Jul 26 '16 14:07 originalmoose

@originalmoose I'm not sure how your issue could have occurred. declare is only required at the top-level, and Typings does not change that unless it's not --global. But when it's not --global, you don't need declare anymore.

blakeembrey avatar Jul 27 '16 06:07 blakeembrey

Sorry, I just re-read - I don't understand the issue now. Typings is literally changing nothing here. Why don't you just write declare in front of module Test like you're meant to before using Typings to install it?

Edit: When I say meant to, if you use tsc without declare it would be an error. In certain IDEs it should warn you. It should be:

declare namespace Test {
    export const enum TestEnum {
        Value = 0,
        Test = 1
    }
}

Notice namespace > module for internal modules.

blakeembrey avatar Jul 27 '16 06:07 blakeembrey

Feel free to continue the discussion if I'm missing something useful here.

blakeembrey avatar Jul 27 '16 06:07 blakeembrey

I think the issue is typelite does not generate the typings correctly, emitting module Test. It should be declare namespace Test as Blake points out.

Probably you can open an issue with typelite

unional avatar Jul 27 '16 06:07 unional

Oh, I missed that typelite was a generator. In that case, I'd recommend bringing up the issue with them.

blakeembrey avatar Jul 27 '16 06:07 blakeembrey

Thanks everyone for the help, I attempted to switch the enum to declare namespace Test but since the enums are in a compiled ts file when I did that I no longer got any output (I normally include the output on the page so I can use the enum globally). I then realized I could have tsc generate a definition for me and by doing so it included the declare modifier. Once I had that working I simply installed that file instead of the .ts and everything worked.

originalmoose avatar Jul 28 '16 13:07 originalmoose