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> types on TypeScript - aka Maybe Types from Flow #23477

Closed
rdsedmundo opened this issue Apr 17, 2018 · 2 comments
Closed

Nullable<T> types on TypeScript - aka Maybe Types from Flow #23477

rdsedmundo opened this issue Apr 17, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@rdsedmundo
Copy link

rdsedmundo commented Apr 17, 2018

I'd like to know if there's any kind of implementation of Nullable types like in C# or what Flow does called as Maybe Types. I haven't found anything here in the repository exactly like that's still opened nor in the documentation.

What I'm using right now is something like: type Nullable<T> = T | undefined | null. But I think a built-in solution for that (even if it would be like this type I'm using) would be more sophisticated. I saw that TypeScript 2.8 is adding NonNullable<T> globally, so I think the inverse has its use cases as well.

@mhegazy
Copy link
Contributor

mhegazy commented Apr 17, 2018

This was discussed in details in #7426 when --strictnull checks were first introduced. the conclusion was that the explicit T | null | undefined were better type names for most users than Nullable<T> or T?, since it is obvious whether it is T | null or T | undefined or T | null | undefined. We have since decided to be out of the business of naming nullable types for users, and let everyone chose a name that makes most sense to their teams.
NonNullable is in contradiction of my previous statement, but the use case here is much more limited than the Nullable, also the type is more involved to write and read, and thus we have opted to add it to the library.

@mhegazy mhegazy added the Duplicate An existing issue was already created label Apr 17, 2018
@rdsedmundo
Copy link
Author

rdsedmundo commented Apr 17, 2018

I understand your point, will close the issue then. I just went through the topic you linked, and I'd still disagree.

It's very verbose doing every time: Nullable<Type> or even worse Type | undefined | null. That's the reason Flow provides that feature using the question mark for example. It's the first feature that I see that Flow has and TypeScript doesn't (I prefer so much TS over Flow). That's sad, but... :)

On my side for example, look the difference between those codes:

export const getBlogModalOpen: StateSelector<boolean> = createSelector(
  getNetworkToConnect,
  equals<NetworkId | undefined | null>('blog'),
);
export const getBlogModalOpen: StateSelector<boolean> = createSelector(
  getNetworkToConnect,
  equals<Nullable<NetworkId>>('blog'),
);
export const getBlogModalOpen: StateSelector<boolean> = createSelector(
  getNetworkToConnect,
  equals<NetworkId?>('blog'),
);

The first case that you said that is 'OK' for the team is, in fact, the worse case. Imagine duplicating this every time. Very unnecessary verbosity.

@microsoft microsoft locked and limited conversation to collaborators Jul 26, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants