-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Normalize union/intersection type combinations #11717
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
tests/cases/compiler/errorMessagesIntersectionTypes04.ts(17,5): error TS2322: Type 'A & B' is not assignable to type 'number'. | ||
tests/cases/compiler/errorMessagesIntersectionTypes04.ts(18,5): error TS2322: Type 'A & B' is not assignable to type 'boolean'. | ||
tests/cases/compiler/errorMessagesIntersectionTypes04.ts(19,5): error TS2322: Type 'A & B' is not assignable to type 'string'. | ||
tests/cases/compiler/errorMessagesIntersectionTypes04.ts(21,5): error TS2322: Type 'number & boolean' is not assignable to type 'string'. | ||
tests/cases/compiler/errorMessagesIntersectionTypes04.ts(21,5): error TS2322: Type '(number & true) | (number & false)' is not assignable to type 'string'. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there some way to present this error as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The actual representation needs to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, type-to-string is what I was thinking of. But you're right, |
||
Type 'number & true' is not assignable to type 'string'. | ||
|
||
|
||
==== tests/cases/compiler/errorMessagesIntersectionTypes04.ts (4 errors) ==== | ||
|
@@ -33,5 +34,6 @@ tests/cases/compiler/errorMessagesIntersectionTypes04.ts(21,5): error TS2322: Ty | |
|
||
str = num_and_bool; | ||
~~~ | ||
!!! error TS2322: Type 'number & boolean' is not assignable to type 'string'. | ||
!!! error TS2322: Type '(number & true) | (number & false)' is not assignable to type 'string'. | ||
!!! error TS2322: Type 'number & true' is not assignable to type 'string'. | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//// [intersectionTypeNormalization.ts] | ||
interface A { a: string } | ||
interface B { b: string } | ||
interface C { c: string } | ||
interface D { d: string } | ||
|
||
// Identical ways of writing the same type | ||
type X1 = (A | B) & (C | D); | ||
type X2 = A & (C | D) | B & (C | D) | ||
type X3 = A & C | A & D | B & C | B & D; | ||
|
||
var x: X1; | ||
var x: X2; | ||
var x: X3; | ||
|
||
interface X { x: string } | ||
interface Y { y: string } | ||
|
||
// Identical ways of writing the same type | ||
type Y1 = (A | X & Y) & (C | D); | ||
type Y2 = A & (C | D) | X & Y & (C | D) | ||
type Y3 = A & C | A & D | X & Y & C | X & Y & D; | ||
|
||
var y: Y1; | ||
var y: Y2; | ||
var y: Y3; | ||
|
||
interface M { m: string } | ||
interface N { n: string } | ||
|
||
// Identical ways of writing the same type | ||
type Z1 = (A | X & (M | N)) & (C | D); | ||
type Z2 = A & (C | D) | X & (M | N) & (C | D) | ||
type Z3 = A & C | A & D | X & (M | N) & C | X & (M | N) & D; | ||
type Z4 = A & C | A & D | X & M & C | X & N & C | X & M & D | X & N & D; | ||
|
||
var z: Z1; | ||
var z: Z2; | ||
var z: Z3; | ||
var z: Z4; | ||
|
||
// Repro from #9919 | ||
|
||
type ToString = { | ||
toString(): string; | ||
} | ||
|
||
type BoxedValue = { kind: 'int', num: number } | ||
| { kind: 'string', str: string } | ||
|
||
type IntersectionFail = BoxedValue & ToString | ||
|
||
type IntersectionInline = { kind: 'int', num: number } & ToString | ||
| { kind: 'string', str: string } & ToString | ||
|
||
function getValueAsString(value: IntersectionFail): string { | ||
if (value.kind === 'int') { | ||
return '' + value.num; | ||
} | ||
return value.str; | ||
} | ||
|
||
//// [intersectionTypeNormalization.js] | ||
var x; | ||
var x; | ||
var x; | ||
var y; | ||
var y; | ||
var y; | ||
var z; | ||
var z; | ||
var z; | ||
var z; | ||
function getValueAsString(value) { | ||
if (value.kind === 'int') { | ||
return '' + value.num; | ||
} | ||
return value.str; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not check the source first here too? I think that's the normal pattern for isRelatedTo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment above the initial
if
statement explains why. For intersection types, the target side is an "each" relationship and the source side is a "some" relationship. We need to deconstruct "each" relationships first to get the correct results.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, so I think I misunderstood the parts of the code that are independent post-distribution. It would work to put intersections before unions now, where it wouldn't before, right?