Skip to content

null | string is not a valid disjoint type #32

@darcyparker

Description

@darcyparker
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.

Activity

bcherny

bcherny commented on Sep 20, 2016

@bcherny
Owner

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 commented on Sep 20, 2016

@bcherny
Owner

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

darcyparker

darcyparker commented on Sep 20, 2016

@darcyparker
ContributorAuthor

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

darcyparker commented on Sep 27, 2016

@darcyparker
ContributorAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @darcyparker@bcherny

        Issue actions

          `null | string` is not a valid disjoint type · Issue #32 · bcherny/json-schema-to-typescript