Skip to content

T? shortcut for T | undefinedΒ #61434

@streamich

Description

@streamich

πŸ” Search Terms

maybe type, optional type, or undefined

βœ… Viability Checklist

⭐ 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

  1. Types which can be something specific or undefined.
  2. APIs which may retrieve nothing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions