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

Strange behavior with union of complex types #23162

Closed
Roaders opened this issue Apr 5, 2018 · 3 comments
Closed

Strange behavior with union of complex types #23162

Roaders opened this issue Apr 5, 2018 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@Roaders
Copy link

Roaders commented Apr 5, 2018

TypeScript Version: 2.8.1

Search Terms:
union complex types

Code

interface IOne{
    stringOne: string;
    numberOne: number;
}

interface ITwo{
    stringTwo: string;
    numberTwo: number;
}

type IEitherOr = IOne | ITwo;

var itemThree: IEitherOr = {
    numberOne: 42343,
    stringOne: "",

    numberTwo: "sdfsd"
}

Expected behavior:
This should throw an error.
IMHO I think that there are 3 issues here.

  • First off I was surprised that numberTwo was allowed AT ALL. I think that the type should be either IOne OR ITwo, not a mix of the two.
  • If we do allowed mixed types (which I don't think we should) then surely if there are any properties for ITwo we should require all of them. This should throw an error unless we have specified both numberTwo and stringTwo
  • lastly it's not even type checking numberTwo. It is typed as a number but it's fine with a string.

Actual behavior:
The compiler is fine with this!

Playground Link:
Link

Work Around:

You can enforce proper type checking on the secondary mixed type like this:

type IFixedEitherOr =
    ( IOne & Partial<ITwo> ) 
    |
    ( ITwo & Partial<IOne> );

var itemFour: IFixedEitherOr = {
    numberOne: 42343,
    stringOne: "",

    numberTwo: "dfgdf" // this errors
}

The code above correctly fails to compile.

@jack-williams
Copy link
Collaborator

Related: #20863

There is nothing unsound about this interaction but it can be undesirable which is why there is excess property checking at initialisation, however I believe this does not work for unions without a discriminant (see related issue).

@Roaders
Copy link
Author

Roaders commented Apr 5, 2018

I really can't see how you can say there is nothing unsound when I have a number property assigned to a string!

@jack-williams
Copy link
Collaborator

The type IEitherOr does not promise that property numberTwo will be a type number. It promises that either:

  • stringOne will be string and numberOne will be number, OR,
  • stringTwo will be string and numberTwo will be number.

If you state IEitherOr using intersection and union (which is roughly equivalent), and consider them like logical and, and logic or. Then you have:

  • ({stringOne: string} & {numberOne: number}) | ({stringTwo: string} & {numberTwo: number})

which can be satisfied with only {stringOne: string} and {numberOne: number}: you never need to claim anything about numberTwo.

Note: you can get unsound behaviour from this object:

if ("numberTwo" in itemThree) {
    itemThree.numberTwo // narrows to ITwo, unsafe!
}

however this is from narrowing rules for union, not assignment compatibility.

@ghost ghost added the Duplicate An existing issue was already created label Apr 5, 2018
@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Duplicate An existing issue was already created labels Apr 5, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants