-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed as not planned
Closed as not planned
Copy link
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
π 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
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β 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
- What do you want to use this for?
Reduce verbosity of having to write " | undefined" in the type annotation - What shortcomings exist with current approaches?
Verbose, especially when there are multiple variables that accept undefined all over the places in many functions - What workarounds are you using in the meantime?
No work around
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created