-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed as not planned
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
π Search Terms
maybe type, optional type, or 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
Allow T? sugar syntax for T | undefined.
π Motivating Example
Often times some API returns a value or undefined, if the value does not exist. For example, consider this "map" interface:
interface Map<K, V> {
get(key: K): V | undefined;
}With this sugar syntax it would be possible to shorten it as folows:
interface Map<K, V> {
get(key: K): V?;
}Or, similarly in observables:
interface Observable<T> {
getValue(): T?;
}Or, built-in Array:
interface Array<T> {
find<S extends T>(predicate): S?;
}And many other places where ... | undefined is used.
π» Use Cases
- Types which can be something specific or undefined.
- APIs which may retrieve nothing.
Metadata
Metadata
Assignees
Labels
DeclinedThe issue was declined as something which matches the TypeScript visionThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScriptAn idea for TypeScript