-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Operator to cast value into literal #26180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Or perhaps a `literalof´keyword? Don't know if it's the correct terminology for the ´false´, ´true´ and ´undefined´ etc.. type, though. |
You can trivially do this with an own function:
Not as nice as compile-time support and adds minimal negligible runtime overhead, but otherwise works fine. |
Aha, I see! Never thought about using a function. Thanks. I still have some I'll have to investigate. Gotta love metaprogramming ¯_(ツ)_/¯ |
What would be even better is somehow being able to get the literal result when doing
|
You can do the reverse, the "widening", already using conditional types:
And with a mapped type you can change all literal types of an interface to their widened versions. |
Duplicate #10195 ? |
@RyanCavanaugh Looks like it! I'll close this one. |
As far as I know, something like this does not exist already in Typescript.
I have a situation like this:
I define functions that are hard coded into my application. However I also want to be able to serve them up and their properties for my end users.
So I want to kill two birds in one stone and have type safety and the actual literal set in one variable.
I do this through typeof
In OperationBlockFunc I do this with the inputs
...in order to cast the arguments to the correct type.
What I would love is some sort of an operator that would cast values into their literal counterparts.
e.g.
Essentially just a shorthand for
foo as foo
The text was updated successfully, but these errors were encountered: