Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nullable<T> built-in type alias #39800

Closed
5 tasks done
dead-claudia opened this issue Jul 29, 2020 · 3 comments
Closed
5 tasks done

Nullable<T> built-in type alias #39800

dead-claudia opened this issue Jul 29, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@dead-claudia
Copy link

Search Terms

nullable type alias
optional type alias
nullable built-in type
optional built-in type

Suggestion

I'd like this added to the list of global built-in types, as an inverse of NonNullable<T>:

type Nullable<T> = T | null | undefined;

Use Cases

  • Sometimes, when I'm coding at the application level, I want a type that can be either null, undefined, or a value, and I don't want to have to pick one.
  • In library definitions, it's quite common for an API to accept both null and undefined as equivalent.
  • It would also make for more easily understood errors and autocomplete, as Nullable<{some: "really", complex: "object", type: true} | number> would be a lot faster and easier to read than {some: "really", complex: "object", type: true} | number | null | undefined.

Examples

Just to name a couple right off:

Chances are, if you've done significant TS development in general applications or if you've done significant type definitions work, you've probably run into something where this could've helped.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code Probably, but not 100% sure.
  • 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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

Duplicate of #39522. Search terms: Nullable (remember to check closed issues)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 29, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@dead-claudia
Copy link
Author

@MartinJohns I did, it just failed me this time around. 😕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants