Skip to content

Commit c6935b4

Browse files
committed
Fix #33732
1 parent 27d9dbe commit c6935b4

6 files changed

Lines changed: 292 additions & 4 deletions

File tree

‎src/compiler/checker.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15582,10 +15582,7 @@ namespace ts {
1558215582
}
1558315583
}
1558415584
}
15585-
if (unionParent && !result && targetIsOptional) {
15586-
result = isRelatedTo(source, undefinedType);
15587-
}
15588-
if (unionParent && !result && reportErrors) {
15585+
if (unionParent && !result) {
1558915586
// The easiest way to get the right errors here is to un-defer (which may be costly)
1559015587
// If it turns out this is too costly too often, we can replicate the error handling logic within
1559115588
// typeRelatedToSomeType without the discriminatable type branch (as that requires a manifest union

‎tests/baselines/reference/excessPropertyCheckWithUnions.errors.txt‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,44 @@ tests/cases/compiler/excessPropertyCheckWithUnions.ts(87,5): error TS2322: Type
155155
!!! error TS2322: Type '{ tag: "button"; type: "submit"; href: string; }' is not assignable to type 'Union'.
156156
!!! error TS2322: Object literal may only specify known properties, and 'href' does not exist in type 'Button'.
157157
};
158+
159+
160+
// Repro from #33732
161+
162+
interface I1 {
163+
prop1: string;
164+
}
165+
166+
interface I2 {
167+
prop2: string;
168+
}
169+
170+
interface I3 extends Record<string, string> {
171+
172+
}
173+
174+
type Properties =
175+
| { [key: string]: never }
176+
| I1
177+
| I2
178+
| I3
179+
;
180+
181+
182+
declare const prop1: string;
183+
declare const prop2: string | undefined;
184+
185+
function F1(_arg: { props: Properties }) { }
186+
F1({
187+
props: {
188+
prop1,
189+
prop2,
190+
},
191+
});
192+
193+
function F2(_props: Properties) { }
194+
F2({
195+
prop1,
196+
prop2,
197+
});
158198

‎tests/baselines/reference/excessPropertyCheckWithUnions.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,46 @@ const obj: Union = {
8787
// should have error here
8888
href: 'foo',
8989
};
90+
91+
92+
// Repro from #33732
93+
94+
interface I1 {
95+
prop1: string;
96+
}
97+
98+
interface I2 {
99+
prop2: string;
100+
}
101+
102+
interface I3 extends Record<string, string> {
103+
104+
}
105+
106+
type Properties =
107+
| { [key: string]: never }
108+
| I1
109+
| I2
110+
| I3
111+
;
112+
113+
114+
declare const prop1: string;
115+
declare const prop2: string | undefined;
116+
117+
function F1(_arg: { props: Properties }) { }
118+
F1({
119+
props: {
120+
prop1,
121+
prop2,
122+
},
123+
});
124+
125+
function F2(_props: Properties) { }
126+
F2({
127+
prop1,
128+
prop2,
129+
});
90130

91131

92132
//// [excessPropertyCheckWithUnions.js]
@@ -144,3 +184,15 @@ var obj = {
144184
// should have error here
145185
href: 'foo'
146186
};
187+
function F1(_arg) { }
188+
F1({
189+
props: {
190+
prop1: prop1,
191+
prop2: prop2
192+
}
193+
});
194+
function F2(_props) { }
195+
F2({
196+
prop1: prop1,
197+
prop2: prop2
198+
});

‎tests/baselines/reference/excessPropertyCheckWithUnions.symbols‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,87 @@ const obj: Union = {
254254

255255
};
256256

257+
258+
// Repro from #33732
259+
260+
interface I1 {
261+
>I1 : Symbol(I1, Decl(excessPropertyCheckWithUnions.ts, 87, 2))
262+
263+
prop1: string;
264+
>prop1 : Symbol(I1.prop1, Decl(excessPropertyCheckWithUnions.ts, 92, 14))
265+
}
266+
267+
interface I2 {
268+
>I2 : Symbol(I2, Decl(excessPropertyCheckWithUnions.ts, 94, 1))
269+
270+
prop2: string;
271+
>prop2 : Symbol(I2.prop2, Decl(excessPropertyCheckWithUnions.ts, 96, 14))
272+
}
273+
274+
interface I3 extends Record<string, string> {
275+
>I3 : Symbol(I3, Decl(excessPropertyCheckWithUnions.ts, 98, 1))
276+
>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --))
277+
278+
}
279+
280+
type Properties =
281+
>Properties : Symbol(Properties, Decl(excessPropertyCheckWithUnions.ts, 102, 1))
282+
283+
| { [key: string]: never }
284+
>key : Symbol(key, Decl(excessPropertyCheckWithUnions.ts, 105, 9))
285+
286+
| I1
287+
>I1 : Symbol(I1, Decl(excessPropertyCheckWithUnions.ts, 87, 2))
288+
289+
| I2
290+
>I2 : Symbol(I2, Decl(excessPropertyCheckWithUnions.ts, 94, 1))
291+
292+
| I3
293+
>I3 : Symbol(I3, Decl(excessPropertyCheckWithUnions.ts, 98, 1))
294+
295+
;
296+
297+
298+
declare const prop1: string;
299+
>prop1 : Symbol(prop1, Decl(excessPropertyCheckWithUnions.ts, 112, 13))
300+
301+
declare const prop2: string | undefined;
302+
>prop2 : Symbol(prop2, Decl(excessPropertyCheckWithUnions.ts, 113, 13))
303+
304+
function F1(_arg: { props: Properties }) { }
305+
>F1 : Symbol(F1, Decl(excessPropertyCheckWithUnions.ts, 113, 40))
306+
>_arg : Symbol(_arg, Decl(excessPropertyCheckWithUnions.ts, 115, 12))
307+
>props : Symbol(props, Decl(excessPropertyCheckWithUnions.ts, 115, 19))
308+
>Properties : Symbol(Properties, Decl(excessPropertyCheckWithUnions.ts, 102, 1))
309+
310+
F1({
311+
>F1 : Symbol(F1, Decl(excessPropertyCheckWithUnions.ts, 113, 40))
312+
313+
props: {
314+
>props : Symbol(props, Decl(excessPropertyCheckWithUnions.ts, 116, 4))
315+
316+
prop1,
317+
>prop1 : Symbol(prop1, Decl(excessPropertyCheckWithUnions.ts, 117, 12))
318+
319+
prop2,
320+
>prop2 : Symbol(prop2, Decl(excessPropertyCheckWithUnions.ts, 118, 14))
321+
322+
},
323+
});
324+
325+
function F2(_props: Properties) { }
326+
>F2 : Symbol(F2, Decl(excessPropertyCheckWithUnions.ts, 121, 3))
327+
>_props : Symbol(_props, Decl(excessPropertyCheckWithUnions.ts, 123, 12))
328+
>Properties : Symbol(Properties, Decl(excessPropertyCheckWithUnions.ts, 102, 1))
329+
330+
F2({
331+
>F2 : Symbol(F2, Decl(excessPropertyCheckWithUnions.ts, 121, 3))
332+
333+
prop1,
334+
>prop1 : Symbol(prop1, Decl(excessPropertyCheckWithUnions.ts, 124, 4))
335+
336+
prop2,
337+
>prop2 : Symbol(prop2, Decl(excessPropertyCheckWithUnions.ts, 125, 10))
338+
339+
});
340+

‎tests/baselines/reference/excessPropertyCheckWithUnions.types‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,3 +312,78 @@ const obj: Union = {
312312

313313
};
314314

315+
316+
// Repro from #33732
317+
318+
interface I1 {
319+
prop1: string;
320+
>prop1 : string
321+
}
322+
323+
interface I2 {
324+
prop2: string;
325+
>prop2 : string
326+
}
327+
328+
interface I3 extends Record<string, string> {
329+
330+
}
331+
332+
type Properties =
333+
>Properties : Properties
334+
335+
| { [key: string]: never }
336+
>key : string
337+
338+
| I1
339+
| I2
340+
| I3
341+
;
342+
343+
344+
declare const prop1: string;
345+
>prop1 : string
346+
347+
declare const prop2: string | undefined;
348+
>prop2 : string | undefined
349+
350+
function F1(_arg: { props: Properties }) { }
351+
>F1 : (_arg: { props: Properties; }) => void
352+
>_arg : { props: Properties; }
353+
>props : Properties
354+
355+
F1({
356+
>F1({ props: { prop1, prop2, },}) : void
357+
>F1 : (_arg: { props: Properties; }) => void
358+
>{ props: { prop1, prop2, },} : { props: { prop1: string; prop2: string | undefined; }; }
359+
360+
props: {
361+
>props : { prop1: string; prop2: string | undefined; }
362+
>{ prop1, prop2, } : { prop1: string; prop2: string | undefined; }
363+
364+
prop1,
365+
>prop1 : string
366+
367+
prop2,
368+
>prop2 : string | undefined
369+
370+
},
371+
});
372+
373+
function F2(_props: Properties) { }
374+
>F2 : (_props: Properties) => void
375+
>_props : Properties
376+
377+
F2({
378+
>F2({ prop1, prop2,}) : void
379+
>F2 : (_props: Properties) => void
380+
>{ prop1, prop2,} : { prop1: string; prop2: string | undefined; }
381+
382+
prop1,
383+
>prop1 : string
384+
385+
prop2,
386+
>prop2 : string | undefined
387+
388+
});
389+

‎tests/cases/compiler/excessPropertyCheckWithUnions.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,43 @@ const obj: Union = {
8787
// should have error here
8888
href: 'foo',
8989
};
90+
91+
92+
// Repro from #33732
93+
94+
interface I1 {
95+
prop1: string;
96+
}
97+
98+
interface I2 {
99+
prop2: string;
100+
}
101+
102+
interface I3 extends Record<string, string> {
103+
104+
}
105+
106+
type Properties =
107+
| { [key: string]: never }
108+
| I1
109+
| I2
110+
| I3
111+
;
112+
113+
114+
declare const prop1: string;
115+
declare const prop2: string | undefined;
116+
117+
function F1(_arg: { props: Properties }) { }
118+
F1({
119+
props: {
120+
prop1,
121+
prop2,
122+
},
123+
});
124+
125+
function F2(_props: Properties) { }
126+
F2({
127+
prop1,
128+
prop2,
129+
});

0 commit comments

Comments
 (0)