Skip to content

Declare local variable as optional #13321

Open

Description

@tinganho

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.

Activity

  1. added
    SuggestionAn idea for TypeScript
    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this feature
    and removed on Jan 6, 2017
  2. lucabrunox commented on Apr 22, 2017

    @lucabrunox

    Or nullable local variables with strictNullChecks enabled.

  3. mhegazy commented on Apr 24, 2017

    @mhegazy
    Contributor

    Or nullable local variables with strictNullChecks enabled.

    let a: string | null;

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

  4. rwyborn commented on Jul 3, 2019

    @rwyborn

    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.

  5. fwd079 commented on Jul 25, 2019

    @fwd079

    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.

  6. StragaSevera commented on Oct 23, 2019

    @StragaSevera

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

  7. adevine commented on Jun 1, 2020

    @adevine

    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;

  8. zolomatok commented on Jul 17, 2022

    @zolomatok

    Has this gone anywhere?

  9. RyanCavanaugh commented on Jul 19, 2022

    @RyanCavanaugh
    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.

  10. victorz commented on Feb 9, 2023

    @victorz

    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.

  11. Levi-Montgomery commented on Aug 1, 2024

    @Levi-Montgomery

    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

  12. fwd079 commented on Aug 2, 2024

    @fwd079

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

  13. daveycodez commented on Dec 9, 2024

    @daveycodez

    RIP! This makes code UGLY

    Fix it.

  14. CosminSontu commented on Jan 23, 2025

    @CosminSontu

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions