You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Reacted by Fawad, Mateo Husselbee, Thomas Jgenti, Ilya Fedorinov, Steven Broderick, Yannic, Philipp Mikheyev, ZHB, opensas, Sumit Chahal and 1 more
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
type Maybe<T> = T | null;
which then lets you do things like let foo: Maybe<string> = null; It's not really saving on characters but I think it reads nicer. Could also do something like
type Nullish<T> = T | null | undefined;
and
type Opt<T> = T | undefined;
Reacted by Karel Kral, Mike Zhou, ZHB, Debajyoti Bhaumik, Alix Royere, Nick R, elmar, Josscii, Killian Huyghe, Maxim Vlasov and 3 more
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.
Reacted by stefnotch, Jesse Thomson, Sean Kelley, ritschwumm, Guillaume Brunerie, Joshua J., trinadhkoya and adevine
Reacted by 0x269, Bosco Domingo, Nick MacFarland, Clint Stevenson, Zoltán Matók, Fawad, Gerson Noboa, RonaldTetsuoCLDF, Vikas Gautam, birgersp and 5 more
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.
Reacted by James Tindal, Guillaume Brunerie and Matthew Walinga
Reacted by Will Stone, adevine, Zoltán Matók, Tingan Ho, Mads, Fawad, RonaldTetsuoCLDF, David Reed, Neli Harbuzava, Ricardo Medina and 8 more
I picked up Typescript after a long pause (~7 years). It's amazing how quickly I got here :).
I think the ? allowed when defining type fields tries to be actually an indicator of the fact that the field was not yet initialized / allowing creating type values without specifying the field / field value.
Coming from other languages (e.g. C#), this generates confuson since the question mark suggests a nullable field.
This means the field accepts null (which is not the case in Typescript).
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 Maybe<T> is employed as adevine mentioned)
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 | null // optionality and nullability (desired sugar syntax)
let name?: string; -> string | undefined
let name??: string; -> string | undefined | null
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?
Activity
lucabrunox commentedon Apr 22, 2017
Or nullable local variables with strictNullChecks enabled.
mhegazy commentedon Apr 24, 2017
?never includesnull.?means "optional" for parameters and properties, meaningT | undefined.rwyborn commentedon Jul 3, 2019
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.
fwd079 commentedon Jul 25, 2019
I just tried using
let s? : string = null;and failed miserably. lolGuess till it is done
let s: string | null;has to suffice. But voting forlet s? : string = null;please.Regards.
StragaSevera commentedon Oct 23, 2019
I fully agree. Please, please, give us terser syntax for the
T | null!adevine commentedon Jun 1, 2020
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
type Maybe<T> = T | null;which then lets you do things like
let foo: Maybe<string> = null;It's not really saving on characters but I think it reads nicer. Could also do something liketype Nullish<T> = T | null | undefined;and
type Opt<T> = T | undefined;zolomatok commentedon Jul 17, 2022
Has this gone anywhere?
RyanCavanaugh commentedon Jul 19, 2022
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.
victorz commentedon Feb 9, 2023
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.
Levi-Montgomery commentedon Aug 1, 2024
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
fwd079 commentedon Aug 2, 2024
5 Years and counting, I guess it's time to unsubscribe from this issue. lol
daveycodez commentedon Dec 9, 2024
RIP! This makes code UGLY
Fix it.
CosminSontu commentedon Jan 23, 2025
I picked up Typescript after a long pause (~7 years). It's amazing how quickly I got here :).
I think the ? allowed when defining type fields tries to be actually an indicator of the fact that the field was not yet initialized / allowing creating type values without specifying the field / field value.
Coming from other languages (e.g. C#), this generates confuson since the question mark suggests a nullable field.
This means the field accepts null (which is not the case in Typescript).
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
Maybe<T>is employed as adevine mentioned)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 | null // optionality and nullability (desired sugar syntax)
let name?: string; -> string | undefined
let name??: string; -> string | undefined | null
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