-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Proposal: Safe type assertion in expression #23104
Comments
Could you expand a bit more on why you would want this? It just seems to be more syntax that needs to be maintained for a functionality that already exists and works. |
example: const obj={
x:"abc"::"abc"|"xyz",
//more props
}; //verbose
const obj:{x:"abc"|"xyz",...}={
x:"abc",
//more props
}; //unsafe
const obj={
x:"abc" as "abc"|"xyz",
//more props
}; I gave a simple example, but I think that it will be more useful in various situations |
Not wild about the As a work around, our project uses const obj = {
x: to<'abc' | 'xyz'>('abc'),
// more props
}; Kind of annoying to have to use this function, though, plus it incurs runtime overhead for no good reason. |
I noticed that |
Same general problem as #7481 |
7481 was closed due to length. Continued at #47920. I figure this might help anyone who lands here get up to speed quicker. |
Background
Currently, it is impossible to add type ascription only to variable declaration.
Also, casting is not safe.
I want a way to safely add type ascription in the middle of the expression.
Proposal
expression :: Type
Example
The text was updated successfully, but these errors were encountered: