Skip to content
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

Open
tinganho opened this issue Jan 6, 2017 · 13 comments
Open

Declare local variable as optional #13321

tinganho opened this issue Jan 6, 2017 · 13 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@tinganho
Copy link
Contributor

tinganho commented Jan 6, 2017

Every time I type an undefinable variable, I think I typed something wrong:

let a: string | undefined;

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?

let a?: string;

The above syntax feel very natural and idiomatic typescript for me.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jan 6, 2017
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Jan 9, 2017
@lucabrunox
Copy link

Or nullable local variables with strictNullChecks enabled.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 24, 2017

Or nullable local variables with strictNullChecks enabled.

let a: string | null;

? never includes null. ? means "optional" for parameters and properties, meaning T | undefined.

@rwyborn
Copy link

rwyborn commented 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
Copy link

fwd079 commented Jul 25, 2019

Every time I type an undefinable variable, I think I typed something wrong:

let a: string | undefined;

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?

let a?: string;

The above syntax feel very natural and idiomatic typescript for me.

I just tried using let s? : string = null; and failed miserably. lol

Guess till it is done let s: string | null; has to suffice. But voting for let s? : string = null; please.

Regards.

@StragaSevera
Copy link

I fully agree. Please, please, give us terser syntax for the T | null!

@adevine
Copy link

adevine commented 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 like

type Nullish<T> = T | null | undefined;

and

type Opt<T> = T | undefined;

@zolomatok
Copy link

Has this gone anywhere?

@RyanCavanaugh
Copy link
Member

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
Copy link

victorz commented 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
Copy link

Levi-Montgomery commented 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 🤓

Consider a more functional programming style

No

@fwd079
Copy link

fwd079 commented Aug 2, 2024

5 Years and counting, I guess it's time to unsubscribe from this issue. lol

@daveycodez
Copy link

RIP! This makes code UGLY

Fix it.

@CosminSontu
Copy link

CosminSontu commented 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests