dangcuuson / graphql-schema-typescript

Generate TypeScript from GraphQL's schema type definitions
191 stars 36 forks source link

`export enum` should be `export const enum` in d.ts files #29

Closed chrisae closed 5 years ago

chrisae commented 5 years ago

Using a const enum gives you the possibility to set

"compilerOptions": {
  "preserveConstEnums": true
}

in the tsconfig.json which will inline used enum values. Otherwise you'll get a runtime error because the enum is not available.

See: https://github.com/Microsoft/TypeScript/issues/1689

dangcuuson commented 5 years ago

I see,

I thought that string enum always generate a corresponding object so I couldn't use enum in declaration files... shame on me :cry:

Thanks for the info, will be updated shortly.

chrisae commented 5 years ago

Thanks for the quick reply and the update :-)

dangcuuson commented 5 years ago

Should be fixed in 1.3.0

I figured out that const enum also works with global declaration, so it's a breaking change (as before graphql enum will be generated as string union under global scope)

Cheers!

dangcuuson commented 5 years ago

Reopen as I realised that export const enum won't generate objects at run time, so any code that uses the enum as run time value will break. e.g: const allRoles = _.values(Roles)

I'll just need to check if user is generating code as type declaration or module :smile: