Skip to content

Commit 9302332

Browse files
authored
Propagate intersectionState in typeRelatedToSomeType (#56207)
1 parent 0abfb52 commit 9302332

6 files changed

+195
-6
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21565,7 +21565,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2156521565
eachTypeRelatedToType(source as UnionType, target, reportErrors && !(source.flags & TypeFlags.Primitive), intersectionState);
2156621566
}
2156721567
if (target.flags & TypeFlags.Union) {
21568-
return typeRelatedToSomeType(getRegularTypeOfObjectLiteral(source), target as UnionType, reportErrors && !(source.flags & TypeFlags.Primitive) && !(target.flags & TypeFlags.Primitive));
21568+
return typeRelatedToSomeType(getRegularTypeOfObjectLiteral(source), target as UnionType, reportErrors && !(source.flags & TypeFlags.Primitive) && !(target.flags & TypeFlags.Primitive), intersectionState);
2156921569
}
2157021570
if (target.flags & TypeFlags.Intersection) {
2157121571
return typeRelatedToEachType(source, target as IntersectionType, reportErrors, IntersectionState.Target);
@@ -21599,7 +21599,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2159921599
let result = Ternary.True;
2160021600
const sourceTypes = source.types;
2160121601
for (const sourceType of sourceTypes) {
21602-
const related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false);
21602+
const related = typeRelatedToSomeType(sourceType, target, /*reportErrors*/ false, IntersectionState.None);
2160321603
if (!related) {
2160421604
return Ternary.False;
2160521605
}
@@ -21608,7 +21608,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2160821608
return result;
2160921609
}
2161021610

21611-
function typeRelatedToSomeType(source: Type, target: UnionOrIntersectionType, reportErrors: boolean): Ternary {
21611+
function typeRelatedToSomeType(source: Type, target: UnionOrIntersectionType, reportErrors: boolean, intersectionState: IntersectionState): Ternary {
2161221612
const targetTypes = target.types;
2161321613
if (target.flags & TypeFlags.Union) {
2161421614
if (containsType(targetTypes, source)) {
@@ -21635,14 +21635,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2163521635
}
2163621636
const match = getMatchingUnionConstituentForType(target as UnionType, source);
2163721637
if (match) {
21638-
const related = isRelatedTo(source, match, RecursionFlags.Target, /*reportErrors*/ false);
21638+
const related = isRelatedTo(source, match, RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
2163921639
if (related) {
2164021640
return related;
2164121641
}
2164221642
}
2164321643
}
2164421644
for (const type of targetTypes) {
21645-
const related = isRelatedTo(source, type, RecursionFlags.Target, /*reportErrors*/ false);
21645+
const related = isRelatedTo(source, type, RecursionFlags.Target, /*reportErrors*/ false, /*headMessage*/ undefined, intersectionState);
2164621646
if (related) {
2164721647
return related;
2164821648
}
@@ -21651,7 +21651,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2165121651
// Elaborate only if we can find a best matching type in the target union
2165221652
const bestMatchingType = getBestMatchingType(source, target, isRelatedTo);
2165321653
if (bestMatchingType) {
21654-
isRelatedTo(source, bestMatchingType, RecursionFlags.Target, /*reportErrors*/ true);
21654+
isRelatedTo(source, bestMatchingType, RecursionFlags.Target, /*reportErrors*/ true, /*headMessage*/ undefined, intersectionState);
2165521655
}
2165621656
}
2165721657
return Ternary.False;

tests/baselines/reference/nestedExcessPropertyChecking.errors.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,29 @@ nestedExcessPropertyChecking.ts(40,9): error TS2559: Type 'false' has no propert
8888
},
8989
},
9090
};
91+
92+
// Repro from #53412
93+
94+
type BaseItem = {
95+
id: number;
96+
}
97+
type ExtendedItem = BaseItem & {
98+
description: string | null
99+
};
100+
101+
type BaseValue = {
102+
// there are other fields
103+
items: BaseItem[];
104+
}
105+
type ExtendedValue = BaseValue & {
106+
// there are other fields
107+
items: ExtendedItem[];
108+
}
109+
110+
const TEST_VALUE: ExtendedValue = {
111+
items: [
112+
{id: 1, description: null},
113+
{id: 2, description: 'wigglytubble'},
114+
]
115+
};
91116

tests/baselines/reference/nestedExcessPropertyChecking.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,31 @@ const response: Query = {
6464
},
6565
},
6666
};
67+
68+
// Repro from #53412
69+
70+
type BaseItem = {
71+
id: number;
72+
}
73+
type ExtendedItem = BaseItem & {
74+
description: string | null
75+
};
76+
77+
type BaseValue = {
78+
// there are other fields
79+
items: BaseItem[];
80+
}
81+
type ExtendedValue = BaseValue & {
82+
// there are other fields
83+
items: ExtendedItem[];
84+
}
85+
86+
const TEST_VALUE: ExtendedValue = {
87+
items: [
88+
{id: 1, description: null},
89+
{id: 2, description: 'wigglytubble'},
90+
]
91+
};
6792

6893

6994
//// [nestedExcessPropertyChecking.js]
@@ -90,3 +115,9 @@ var response = {
90115
},
91116
},
92117
};
118+
var TEST_VALUE = {
119+
items: [
120+
{ id: 1, description: null },
121+
{ id: 2, description: 'wigglytubble' },
122+
]
123+
};

tests/baselines/reference/nestedExcessPropertyChecking.symbols

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,56 @@ const response: Query = {
165165
},
166166
};
167167

168+
// Repro from #53412
169+
170+
type BaseItem = {
171+
>BaseItem : Symbol(BaseItem, Decl(nestedExcessPropertyChecking.ts, 62, 2))
172+
173+
id: number;
174+
>id : Symbol(id, Decl(nestedExcessPropertyChecking.ts, 66, 17))
175+
}
176+
type ExtendedItem = BaseItem & {
177+
>ExtendedItem : Symbol(ExtendedItem, Decl(nestedExcessPropertyChecking.ts, 68, 1))
178+
>BaseItem : Symbol(BaseItem, Decl(nestedExcessPropertyChecking.ts, 62, 2))
179+
180+
description: string | null
181+
>description : Symbol(description, Decl(nestedExcessPropertyChecking.ts, 69, 32))
182+
183+
};
184+
185+
type BaseValue = {
186+
>BaseValue : Symbol(BaseValue, Decl(nestedExcessPropertyChecking.ts, 71, 2))
187+
188+
// there are other fields
189+
items: BaseItem[];
190+
>items : Symbol(items, Decl(nestedExcessPropertyChecking.ts, 73, 18))
191+
>BaseItem : Symbol(BaseItem, Decl(nestedExcessPropertyChecking.ts, 62, 2))
192+
}
193+
type ExtendedValue = BaseValue & {
194+
>ExtendedValue : Symbol(ExtendedValue, Decl(nestedExcessPropertyChecking.ts, 76, 1))
195+
>BaseValue : Symbol(BaseValue, Decl(nestedExcessPropertyChecking.ts, 71, 2))
196+
197+
// there are other fields
198+
items: ExtendedItem[];
199+
>items : Symbol(items, Decl(nestedExcessPropertyChecking.ts, 77, 34))
200+
>ExtendedItem : Symbol(ExtendedItem, Decl(nestedExcessPropertyChecking.ts, 68, 1))
201+
}
202+
203+
const TEST_VALUE: ExtendedValue = {
204+
>TEST_VALUE : Symbol(TEST_VALUE, Decl(nestedExcessPropertyChecking.ts, 82, 5))
205+
>ExtendedValue : Symbol(ExtendedValue, Decl(nestedExcessPropertyChecking.ts, 76, 1))
206+
207+
items: [
208+
>items : Symbol(items, Decl(nestedExcessPropertyChecking.ts, 82, 35))
209+
210+
{id: 1, description: null},
211+
>id : Symbol(id, Decl(nestedExcessPropertyChecking.ts, 84, 9))
212+
>description : Symbol(description, Decl(nestedExcessPropertyChecking.ts, 84, 15))
213+
214+
{id: 2, description: 'wigglytubble'},
215+
>id : Symbol(id, Decl(nestedExcessPropertyChecking.ts, 85, 9))
216+
>description : Symbol(description, Decl(nestedExcessPropertyChecking.ts, 85, 15))
217+
218+
]
219+
};
220+

tests/baselines/reference/nestedExcessPropertyChecking.types

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,58 @@ const response: Query = {
160160
},
161161
};
162162

163+
// Repro from #53412
164+
165+
type BaseItem = {
166+
>BaseItem : { id: number; }
167+
168+
id: number;
169+
>id : number
170+
}
171+
type ExtendedItem = BaseItem & {
172+
>ExtendedItem : BaseItem & { description: string | null; }
173+
174+
description: string | null
175+
>description : string | null
176+
177+
};
178+
179+
type BaseValue = {
180+
>BaseValue : { items: BaseItem[]; }
181+
182+
// there are other fields
183+
items: BaseItem[];
184+
>items : BaseItem[]
185+
}
186+
type ExtendedValue = BaseValue & {
187+
>ExtendedValue : BaseValue & { items: ExtendedItem[]; }
188+
189+
// there are other fields
190+
items: ExtendedItem[];
191+
>items : ExtendedItem[]
192+
}
193+
194+
const TEST_VALUE: ExtendedValue = {
195+
>TEST_VALUE : ExtendedValue
196+
>{ items: [ {id: 1, description: null}, {id: 2, description: 'wigglytubble'}, ]} : { items: ({ id: number; description: null; } | { id: number; description: string; })[]; }
197+
198+
items: [
199+
>items : ({ id: number; description: null; } | { id: number; description: string; })[]
200+
>[ {id: 1, description: null}, {id: 2, description: 'wigglytubble'}, ] : ({ id: number; description: null; } | { id: number; description: string; })[]
201+
202+
{id: 1, description: null},
203+
>{id: 1, description: null} : { id: number; description: null; }
204+
>id : number
205+
>1 : 1
206+
>description : null
207+
208+
{id: 2, description: 'wigglytubble'},
209+
>{id: 2, description: 'wigglytubble'} : { id: number; description: string; }
210+
>id : number
211+
>2 : 2
212+
>description : string
213+
>'wigglytubble' : "wigglytubble"
214+
215+
]
216+
};
217+

tests/cases/compiler/nestedExcessPropertyChecking.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,28 @@ const response: Query = {
6363
},
6464
},
6565
};
66+
67+
// Repro from #53412
68+
69+
type BaseItem = {
70+
id: number;
71+
}
72+
type ExtendedItem = BaseItem & {
73+
description: string | null
74+
};
75+
76+
type BaseValue = {
77+
// there are other fields
78+
items: BaseItem[];
79+
}
80+
type ExtendedValue = BaseValue & {
81+
// there are other fields
82+
items: ExtendedItem[];
83+
}
84+
85+
const TEST_VALUE: ExtendedValue = {
86+
items: [
87+
{id: 1, description: null},
88+
{id: 2, description: 'wigglytubble'},
89+
]
90+
};

0 commit comments

Comments
 (0)