From 4297c1a6a9321af8eefcb84eec4293ffa9a4bf31 Mon Sep 17 00:00:00 2001 From: Turadg Aleahmad Date: Fri, 18 Oct 2024 10:56:54 -0700 Subject: [PATCH] docs: example of enum in JSDoc --- .../documentation/copy/en/reference/Enums.md | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/documentation/copy/en/reference/Enums.md b/packages/documentation/copy/en/reference/Enums.md index 7e5e23b625e8..6101da2318be 100644 --- a/packages/documentation/copy/en/reference/Enums.md +++ b/packages/documentation/copy/en/reference/Enums.md @@ -443,4 +443,24 @@ walk(EDirection.Left); run(ODirection.Right); ``` +In plain JavaScript syntax: +```js +/** + * @enum {typeof Direction[keyof typeof Direction]} + */ +const Direction = /** @type {const} */({ + Up: 'up', + Down: 'down', + Left: 'left', + Right: 'right', +}); + +/** @type {Direction} */ +const valid = 'up'; + +/** @type {Direction} */ +// @ts-expect-error +const bad = 'sideways'; +``` + The biggest argument in favour of this format over TypeScript's `enum` is that it keeps your codebase aligned with the state of JavaScript, and [when/if](https://github.com/rbuckton/proposal-enum) enums are added to JavaScript then you can move to the additional syntax.