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

Standardised Nullable<T> type #49984

Closed
5 tasks done
J-Cake opened this issue Jul 21, 2022 · 4 comments
Closed
5 tasks done

Standardised Nullable<T> type #49984

J-Cake opened this issue Jul 21, 2022 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@J-Cake
Copy link

J-Cake commented Jul 21, 2022

Suggestion

Primary Suggestion

I'm not sure why this hasn't been standardised yet, I'm sure there's a good explanation, but basically, the idea is that

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

be introduced into the standard list of types.

Secondary Suggestion

  • I also happened to notice that variables cannot be marked with ? directly. My suggestion would be that
let x?: number;

translates to

let x: Nullable<number>;

List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.

✅ Viability Checklist

My suggestion meets these guidelines:

  • 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 feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Basically I want to be able to do:

const x?: Date = Api?.getTime();
  • It's much cleaner
  • I use exactly that type in every single project
  • I tend to redefine it in most of my files instead of importing it because I can't think of a sensible place to put it
  • me want :P
@MartinJohns
Copy link
Contributor

Duplicate of #39522 and many others. Used search terms: nullable in:title

@MartinJohns
Copy link
Contributor

Your secondary suggestion is a duplicate of #41901. Used search terms: optional variable in:title

@MartinJohns
Copy link
Contributor

I tend to redefine it in most of my files instead of importing it because I can't think of a sensible place to put it

If you declare the type in a non-module declaration file it will be available everywhere without having to import it.

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Jul 21, 2022
@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.

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