Get rid of string enums, use union types instead
See original GitHub issueOur string enums, like those used for *EventType
, are code that is redundant and only used for type checking. Instead, we could use union types that ensure types just as well, without adding payload to the build.
The easiest way to get documented event types in JSDoc while making TypeScript turn them into union types is a @typedef
in JSDoc with a keyof
operator in TypeScript:
/**
* @typedef {Object} MapBrowserEventTypeKeys
* @property {string} singleclick A true single click with no dragging and no double click. Note that this
* event is delayed by 250 ms to ensure that it is not a double click.
* @property {string} click A click with no dragging. A double click will fire two of this.
* @property {string} dblclick A true double click, with no dragging.
* @property {string} pointerdrag Triggered when a pointer is dragged.
* @property {string} pointermove Triggered when a pointer is moved. Note that on touch devices this is
* triggered when the map is panned, so is not the same as mousemove.
*/
/**
* @typedef {keyof MapBrowserEventTypeKeys} MapBrowserEventType
*/
/** @type {MapBrowserEventType} */
const a = 'pointerdrag';
See also https://blog.bam.tech/developer-news/should-you-use-enums-or-union-types-in-typescript.
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Typescript has unions, so are enums redundant?
With the recent versions of TypeScript, it is easy to declare iterable union types. Therefore, you should prefer union types to enums.
Read more >Tidy TypeScript: Prefer union types over enums - fettblog.eu
A simple union type gives you something that works similarly and is much more aligned with TypeScript. ... You get all the benefits...
Read more >TypeScript | Union Types vs Enums
Learn the difference between using union types and enums in TypeScript with simple examples, as well as understanding when to use one or...
Read more >Union Types vs. Enums in TypeScript
Conceptually, enums can be a subset of union types with numeric or string values. Numeric enums are less safe. If you call updateStatus(20)...
Read more >Should You Use Enums or Union Types in Typescript?
To use it, a modern editor like VS code will give autocompletion in both cases. But enums need to be exported and imported,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I am wondering if the
*EventType.js
files could be integrated into the*Event.js
files at that go? I’d like to look into this issue after I finished https://github.com/openlayers/openlayers/pull/12430.I like the idea of not using
keyof
and@fires
. It will probably cause less issues with typing generation. Removing the jsdoc plugins is great as well, it makes it easier to find where the actual documentation texts are. Also it reduces the dependency on jsdoc, which is not developed much anymore.