-
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
Optional local variables #41901
Comments
But the variable in your example is not optional. It's always defined. You're confusing optional with "may be undefined". |
@MartinJohns I am not confusing - that's exactly what I want. No matter how you name it, it would extend the already existing syntax and make it useful for local variables as well. You already can use it for class variables aka members/properties, so why not local as well |
It would complicate the implementation of #13195, if it ever is to be added. Optional properties and arguments can be missing, not only |
I don't think it makes sense to put the |
I also support let name: string? to be equivalent to let name:string|undefined|null. So as of current latest version, is this actually implemented? |
Including
No, and it (most likely) never will be implemented. The feature has been declined. |
Wonder why is this a bad thing, such that it is not implemented. verbosity can be greatly reduced if implemented. I mean if using question mark to represent undefined|null can cause confusion, then other characters can be considered. |
I think this is a great idea. let result?: boolean; It's obvious that Now, @MartinJohns argued that supporting this would complicate #13195 – meanwhile, on Jun 1, 2021 that issue was actually resolved. Does this mean that in principle, TypeScript could support this syntax? I totally agree with @angryziber on this: let result: boolean | undefined = undefined // too verbose And there's good code to be written that uses this syntax. In the example above, we can imagine that there is no result yet, neither success or failure. |
Suggestion
TypeScript already supports optional parameters and fields with a question mark.
Why not extend this feature to local variables as well, to allow them having an undefined value, e.g.
Use Cases/Examples
in this case specifying
number
type is also optional:This would complete the usage of question mark
?
as nullablity/unefineability operator, similar to Kotlin.Currently, defining an optional variable requires writing a lot of noise, e.g.
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: