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

Type inference of the type union and intersection #22632

Closed
AntoineEsteve opened this issue Mar 16, 2018 · 2 comments
Closed

Type inference of the type union and intersection #22632

AntoineEsteve opened this issue Mar 16, 2018 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@AntoineEsteve
Copy link

TypeScript Version: 2.7.2

Search Terms: Type union, type intersection, type inference

Code

interface A {
    a: boolean
}

interface A1 extends A {
    a1: boolean
}

interface A2 extends A {
    a2: boolean
}

type A1andA2 = A1 & A2

type A1orA2 = A1 | A2

type A1orA2orA1andA2 = A1orA2 | A1andA2

// Expected
const test1: A1orA2 = {
    a: true,
    a1: true,
}

console.log(test1) // Type inference: A1

// Expected
const test2: A1orA2 = {
    a: true,
    a2: true,
}

console.log(test2) // Type inference: A2

// Expected
const test3: A1orA2orA1andA2 = {
    a: true,
    a1: true,
    a2: true,
}

console.log(test3) // Type inference: A1andA2 <<< good

// Expected => union accept the intersections
const test4: A1orA2 = {
    a: true,
    a1: true,
    a2: true,
}

console.log(test4) // Type inference: A1 | A2 <<< Should be A1 & A2, not good
console.log(test4.a1) // Error 
console.log(test4.a2) // Error

It is not very clear in the documentation if the union contains the intersection, but both assumptions seems to have issues

Expected behavior:

  • Assumption: union contains the intersection
    A1 | A2 equals A1 or A2 or A1 & A2: the type intefence of { a: true, a1: true, a2: true } should be A1 & A2
  • Assumption: union excludes the intersection
    A1 | A2 equals A1 or A2 excluding A1 & A2: { a: true, a1: true, a2: true } should not be assignable to A1 | A2

Actual behavior:

  • Assumption: union contains the intersection
    The type inference of { a: true, a1: true, a2: true } is A1 | A2 and we can not use the properties a1 and a2 without type guards
  • Assumption: union excludes the intersection
    { a: true, a1: true, a2: true } is still assignable to A1 | A2

Playground Link:
https://www.typescriptlang.org/play/index.html#src=interface%20A%20%7B%0D%0A%20%20%20%20a%3A%20boolean%0D%0A%7D%0D%0A%0D%0Ainterface%20A1%20extends%20A%20%7B%0D%0A%20%20%20%20a1%3A%20boolean%0D%0A%7D%0D%0A%0D%0Ainterface%20A2%20extends%20A%20%7B%0D%0A%20%20%20%20a2%3A%20boolean%0D%0A%7D%0D%0A%0D%0Atype%20A1andA2%20%3D%20A1%20%26%20A2%0D%0A%0D%0Atype%20A1orA2%20%3D%20A1%20%7C%20A2%0D%0A%0D%0Atype%20A1orA2orA1andA2%20%3D%20A1orA2%20%7C%20A1andA2%0D%0A%0D%0A%2F%2F%20Expected%0D%0Aconst%20test1%3A%20A1orA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a1%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test1)%20%2F%2F%20Type%20inference%3A%20A1%0D%0A%0D%0A%2F%2F%20Expected%0D%0Aconst%20test2%3A%20A1orA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a2%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test2)%20%2F%2F%20Type%20inference%3A%20A2%0D%0A%0D%0A%2F%2F%20Expected%0D%0Aconst%20test3%3A%20A1orA2orA1andA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a1%3A%20true%2C%0D%0A%20%20%20%20a2%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test3)%20%2F%2F%20Type%20inference%3A%20A1andA2%20%3C%3C%3C%20good%0D%0A%0D%0A%2F%2F%20Expected%20%3D%3E%20union%20accept%20the%20intersections%0D%0Aconst%20test4%3A%20A1orA2%20%3D%20%7B%0D%0A%20%20%20%20a%3A%20true%2C%0D%0A%20%20%20%20a1%3A%20true%2C%0D%0A%20%20%20%20a2%3A%20true%2C%0D%0A%7D%0D%0A%0D%0Aconsole.log(test4)%20%2F%2F%20Type%20inference%3A%20A1%20%7C%20A2%20%3C%3C%3C%20Should%20be%20A1%20%26%20A2%2C%20not%20good%0D%0Aconsole.log(test4.a1)%20%2F%2F%20Error%20%0D%0Aconsole.log(test4.a2)%20%2F%2F%20Error

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Mar 16, 2018
@jack-williams
Copy link
Collaborator

jack-williams commented Mar 19, 2018

Union includes the intersection, otherwise you would get weird behaviour such as 0 not being assignable to 0 | number. The operator behaves much the same as the set operation.

This issue seems to be similar to #20863.

I wouldn't expect the inferred type of test4 to be A1 & A2 because of the structural nature of TypeScript. Partitioning the inferred type into A1 and A2 is arbitrary and it would be equally valid to infer {a: boolean; a1: boolean; a2; boolean}. You get this behaviour for test3 because you include it explicitly in the union type and it uses discrimination to remove A1orA2.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@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
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants