-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Declare local variable as optional #13321
Comments
Or nullable local variables with strictNullChecks enabled. |
let a: string | null;
|
This is absolutely needed. Maintaining code that deals with a lot of optional params/members is painful to jump back and forth between ? and type | undefined when in reality you are dealing with the exact same objects in both cases (just sometimes as params/members, and other times as locals). I suspect people aren't bashing down the walls on this one because strictNullChecks usage isn't as common as it should be. C# is a good example where the nullable ? modifier is equally applicable to params, locals and members. |
I just tried using Guess till it is done Regards. |
I fully agree. Please, please, give us terser syntax for the |
While this is an old issue I just thought I'd leave a note here in case anyone else finds this. A lot of libraries make this optional syntax nicer using generics. For example, defining Maybe as
which then lets you do things like
and
|
Has this gone anywhere? |
I don't think we're likely to implement this. It's outside the defined grammar for the types-as-comments proposal and it's very confusing, semantically speaking - the variable is absolutely there, just maybe not initialized, whereas an optional property might very well be truly missing. |
Consider a more functional programming style to reduce the need to have lots of variables that are declared before their values are defined. Aim for immutability as much as possible. |
Swift has this and as a consequence you got to safely unwrap it each time you want to use it. I don't think that's something I want. If we borrow anything from swift, it should be the ability to use emojis as variable names 🤓
No |
5 Years and counting, I guess it's time to unsubscribe from this issue. lol |
RIP! This makes code UGLY Fix it. |
I picked up Typescript after a long pause (~7 years). It's amazing how quickly I got here :). Coming from other languages (e.g. C#), this generates confuson since the question mark suggests a nullable field. I understand the worry for addressing this si possiblity of further confusion betweend field optionality and its nullability. Curently the issue is that one cannot specify an optional nullable field (unless I see two ways to address this... of course, maybe there are others, but here goes: let name?: string; -> string | undefined // optionality (current behavior) let name?: string; -> string | undefined of course the syntax would be identical for fields and let statements... Would something like this solve the issue? Are there any constraints (nuances I'm missing) that would prevent something like this being added to the language? Thanks |
Every time I type an undefinable variable, I think I typed something wrong:
Why do I think I did something wrong? It's because I never add
undefined
type to a union type as above except in variable declarations.Don't you think it is time, introduce the optional operator to local variable declarations as well?
The above syntax feel very natural and idiomatic typescript for me.
The text was updated successfully, but these errors were encountered: