Skip to content

Support alias for undefined in type annotation for local variable in a functionΒ #62498

@hannah23280

Description

@hannah23280

πŸ” Search Terms

Typescript: v5.9.2

Current Situation:
To define a local variable that can hold either a string or undefined value:

function() {
    let prop: string | undefined = undefined
}

βœ… Viability Checklist

⭐ Suggestion

Proposed enhancement

  • Replace " | undefined" in the type annotation, with "?" suffixed to the variable name
function () {
    let prop?: string = undefined
}

The enhancement reduces verbosity and also align with the class property, as shown below

class A{
     prop?: string = undefined    //this syntax is currently supported for class property, but not local variable of function
}

πŸ“ƒ Motivating Example

NA

πŸ’» Use Cases

  1. What do you want to use this for?
    Reduce verbosity of having to write " | undefined" in the type annotation
  2. What shortcomings exist with current approaches?
    Verbose, especially when there are multiple variables that accept undefined all over the places in many functions
  3. What workarounds are you using in the meantime?
    No work around

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions