Skip to content

Use union type aliases instead of const enums - #5

Merged
lucaswerkmeister merged 1 commit into
masterfrom
no-enum
Jun 8, 2020
Merged

lucaswerkmeister merged 1 commit into
masterfrom
no-enum

Conversation

@lucaswerkmeister

Copy link
Copy Markdown
Member

We don’t want to use const enums, for two reasons:

  1. It seems to be generally discouraged to share const enums across module boundaries; they are apparently mostly intended as an internal mechanism. (I have yet to find any documentation that tells you this, but this comment seems to imply we’re supposed to understand this naturally.)

  2. Babel’s TypeScript transform plugin does not support them. There is a plugin to transform const enums into non-const ones, but it seems to be intended for your own code, not for dependencies. This means that Data Bridge can’t use this module as long as it uses const enums, because they will not be inlined at compile time, but rather be sought for at run time, when they don’t exist.

With type aliases for union types, we lose the possibility for declaration merging, but it’s unclear how significant this actually was. If we (the Wikidata team) need more data (value) types, it’ll make more sense to add them upstream (here) anyways, rather than augmenting the declarations in our downstream projects.

(Side note: the augmentation syntax in the README.md was incorrect anyways – a namespace name must be an identifier, not a string literal. An ambient module would have been closer to the real syntax.)

We don’t want to use const enums, for two reasons:

1. It seems to be generally discouraged to share const enums across
   module boundaries; they are apparently mostly intended as an internal
   mechanism. (I have yet to find any documentation that tells you this,
   but [1] seems to imply we’re supposed to understand this naturally.)

2. Babel’s TypeScript transform plugin does not support them [2]. There
   is a plugin [3] to transform const enums into non-const ones, but it
   seems to be intended for your own code, not for dependencies. This
   means that Data Bridge can’t use this module as long as it uses const
   enums, because they will not be inlined at compile time, but rather
   be sought for at run time, when they don’t exist.

With type aliases for union types, we lose the possibility for
declaration merging, but it’s unclear how significant this actually was.
If we (the Wikidata team) need more data (value) types, it’ll make more
sense to add them upstream (here) anyways, rather than augmenting the
declarations in our downstream projects.

(Side note: the augmentation syntax in the README.md was incorrect
anyways – a namespace name must be an identifier, not a string literal.
An ambient module would have been closer to the real syntax.)

[1]: microsoft/TypeScript#37179 (comment)
[2]: babel/babel#8741
[3]: https://github.com/dosentmatter/babel-plugin-const-enum
@tzhelyazkova

Copy link
Copy Markdown

We don’t want to use const enums, for two reasons:
1. It seems to be generally discouraged to share const enums across module boundaries; they are apparently mostly intended as an internal mechanism. (I have yet to find any documentation that tells you this, but this comment seems to imply we’re supposed to understand this naturally.)

This article seems to support the statement that it's generally discouraged to use const enums.

Comment thread README.md
```

[declaration merging]: https://www.typescriptlang.org/docs/handbook/declaration-merging.html
Feel free to send pull requests for other data (value) types.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lucaswerkmeister

Copy link
Copy Markdown
Member Author

This article seems to support the statement that it's generally discouraged to use const enums.

Thanks – maybe we should try this as well:

TypeScript has an isolatedModules compiler option that performs additional checks to ensure that the compiled code is safe for this type of compilation process. If you are are writing a library, you should enable this option to ensure that you are not exporting const enum declarations and that all TypeScript developers can compile code that imports your library.

@lucaswerkmeister
lucaswerkmeister merged commit c24e9c1 into master Jun 8, 2020
@lucaswerkmeister
lucaswerkmeister deleted the no-enum branch June 8, 2020 13:10
@lucaswerkmeister

Copy link
Copy Markdown
Member Author

See #6 for isolatedModules.

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants