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

null | string is not a valid disjoint type #32

Closed
darcyparker opened this issue Sep 20, 2016 · 4 comments
Closed

null | string is not a valid disjoint type #32

darcyparker opened this issue Sep 20, 2016 · 4 comments

Comments

@darcyparker
Copy link
Contributor

I made a mistake in part of the test case requested for #27.

anotherValue?: null | string;
is not valid.

Unions of other simple types are being handled nicely - thank you again for adding this capability. But I didn't realize until having tsc complain and then reading How to declare a type as nullable in TypeScript? (which coincidentally @bcherny submitted an answer) that the anotherValue?: null | string; is not valid typescript. After reading the answers to this stackoverflow question I realize that anytime there are disjoint types for a property that includes null, then null should be filtered from the list of types and the property should become optional (which in the example test case coincidentally was).

Would you agree this is a correct interpretation of how to translate a disjoint type with null in its list and transforming to a typescript interface? (If not, it's still a bug because null | string is not valid according to tsc. So I am open to other suggestions of how to handle disjoint types with null in it.)

BTW: I saw this deeper in the comments of the above stackoverflow question: microsoft/TypeScript#7140. So it looks like this is just a bug with tsc < 2.0.

@bcherny
Copy link
Owner

bcherny commented Sep 20, 2016

This is not a bug, but a behavior in TS<2.

  • In TS<2, void is subtype of all types, and there are no named null and undefined types
  • In TS>=2 in strict null check mode:
    • void is still a subtype of all types
    • never is a subtype of all types
    • null is a subtype of any
    • undefined is a subtype of any and void

At most, we should add a readme note here. Biting my tongue, I expect that in a few months most people will have switched onto TS2.

@bcherny bcherny closed this as completed Sep 20, 2016
@bcherny
Copy link
Owner

bcherny commented Sep 20, 2016

If you're curious, see microsoft/TypeScript#7140 and microsoft/TypeScript#8652 - these have not yet made it into the language spec.

@darcyparker
Copy link
Contributor Author

Thanks for the explaination.
I plan to move to 2.0 as soon as I can. In the meantime, tsc is logging an error for me. I worked around it on my side by changing the options for the code generator that creates the JSON schema I am inputting.

@darcyparker
Copy link
Contributor Author

FYI: If someone comes across this, I am on typescript 2.0.3 and I can confirm this is no longer an issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants