Replies: 1 comment 1 reply
-
How could we take advantage of the already declared I refer to this pattern:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I didn't find a topic on this in here so I'm adding it.
TypeScript 3.4 introduced the concept of "const assertions" through microsoft/TypeScript#29510
Turns out that we can actually make use of this in the very same way as we do type casting/type assertions in JSDoc (as showed in #18) and just like in TS simply use
const
instead of a type (as documented here):Example taken from @AustinGil here: microsoft/TypeScript#30445 (comment)
This also works for eg. objects:
This can help with eg. literal inference and in the creation of enum-style definitions.
Previously I at times felt the need to separately define the type to get a similar type to work with:
Simply doing
const obj2 = /** @type {const} */ ({ foo: 'bar', abc: 123 })
is much easier and more DRY.Beta Was this translation helpful? Give feedback.
All reactions