-
Notifications
You must be signed in to change notification settings - Fork 413
Description
I made a mistake in part of the test case requested for #27.
anotherValue?: null | string; |
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
.
Activity
bcherny commentedon Sep 20, 2016
This is not a bug, but a behavior in TS<2.
null
andundefined
typesnever
is a subtype of all typesnull
is a subtype ofany
undefined
is a subtype ofany
andvoid
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 commentedon 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 commentedon Sep 20, 2016
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 commentedon Sep 27, 2016
FYI: If someone comes across this, I am on typescript 2.0.3 and I can confirm this is no longer an issue.