question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get rid of string enums, use union types instead

See original GitHub issue

Our 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:closed
  • Created 2 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
simonseyockcommented, Jun 22, 2021

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.

0reactions
simonseyockcommented, Sep 6, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found