Skip to content

Commit 22bee77

Browse files
authored
Properly make inferences from partial source type (#42038)
* Slightly less picky check in typesDefinitelyUnrelated * Accept new baselines * Add regression test
1 parent 777eb84 commit 22bee77

8 files changed

+130
-11
lines changed

src/compiler/checker.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20045,7 +20045,7 @@ namespace ts {
2004520045
// Two object types that each have a property that is unmatched in the other are definitely unrelated.
2004620046
return isTupleType(source) && isTupleType(target) ? tupleTypesDefinitelyUnrelated(source, target) :
2004720047
!!getUnmatchedProperty(source, target, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true) &&
20048-
!!getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true);
20048+
!!getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false);
2004920049
}
2005020050

2005120051
function getTypeFromInference(inference: InferenceInfo) {

tests/baselines/reference/genericRestParameters3.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(37,21): error TS234
2323
Property '0' is missing in type 'CoolArray<any>' but required in type '[cb: (...args: any[]) => void]'.
2424
tests/cases/conformance/types/rest/genericRestParameters3.ts(44,32): error TS2345: Argument of type '[10, 20]' is not assignable to parameter of type 'CoolArray<number>'.
2525
Property 'hello' is missing in type '[10, 20]' but required in type 'CoolArray<number>'.
26-
tests/cases/conformance/types/rest/genericRestParameters3.ts(49,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<unknown>'.
27-
Property 'hello' is missing in type '[]' but required in type 'CoolArray<unknown>'.
26+
tests/cases/conformance/types/rest/genericRestParameters3.ts(49,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
27+
Property 'hello' is missing in type '[]' but required in type 'CoolArray<never>'.
2828
tests/cases/conformance/types/rest/genericRestParameters3.ts(50,5): error TS2345: Argument of type '[number]' is not assignable to parameter of type 'CoolArray<unknown>'.
2929
Property 'hello' is missing in type '[number]' but required in type 'CoolArray<unknown>'.
3030
tests/cases/conformance/types/rest/genericRestParameters3.ts(51,5): error TS2345: Argument of type '[number, number]' is not assignable to parameter of type 'CoolArray<unknown>'.
@@ -124,8 +124,8 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(59,5): error TS2345
124124

125125
baz(); // Error
126126
~~~~~
127-
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<unknown>'.
128-
!!! error TS2345: Property 'hello' is missing in type '[]' but required in type 'CoolArray<unknown>'.
127+
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
128+
!!! error TS2345: Property 'hello' is missing in type '[]' but required in type 'CoolArray<never>'.
129129
!!! related TS2728 tests/cases/conformance/types/rest/genericRestParameters3.ts:30:5: 'hello' is declared here.
130130
baz(1); // Error
131131
~
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
tests/cases/compiler/inferenceFromIncompleteSource.ts(11,11): error TS2345: Argument of type '{ items: { name: string; }[]; itemKey: "name"; }' is not assignable to parameter of type 'ListProps<{ name: string; }, "name">'.
2+
Property 'prop' is missing in type '{ items: { name: string; }[]; itemKey: "name"; }' but required in type 'ListProps<{ name: string; }, "name">'.
3+
4+
5+
==== tests/cases/compiler/inferenceFromIncompleteSource.ts (1 errors) ====
6+
// Repro from #42030
7+
8+
interface ListProps<T, K extends keyof T> {
9+
items: T[];
10+
itemKey: K;
11+
prop: number;
12+
}
13+
14+
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
15+
16+
Component({items: [{name:' string'}], itemKey: 'name' });
17+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18+
!!! error TS2345: Argument of type '{ items: { name: string; }[]; itemKey: "name"; }' is not assignable to parameter of type 'ListProps<{ name: string; }, "name">'.
19+
!!! error TS2345: Property 'prop' is missing in type '{ items: { name: string; }[]; itemKey: "name"; }' but required in type 'ListProps<{ name: string; }, "name">'.
20+
!!! related TS2728 tests/cases/compiler/inferenceFromIncompleteSource.ts:6:3: 'prop' is declared here.
21+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//// [inferenceFromIncompleteSource.ts]
2+
// Repro from #42030
3+
4+
interface ListProps<T, K extends keyof T> {
5+
items: T[];
6+
itemKey: K;
7+
prop: number;
8+
}
9+
10+
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
11+
12+
Component({items: [{name:' string'}], itemKey: 'name' });
13+
14+
15+
//// [inferenceFromIncompleteSource.js]
16+
"use strict";
17+
// Repro from #42030
18+
Component({ items: [{ name: ' string' }], itemKey: 'name' });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=== tests/cases/compiler/inferenceFromIncompleteSource.ts ===
2+
// Repro from #42030
3+
4+
interface ListProps<T, K extends keyof T> {
5+
>ListProps : Symbol(ListProps, Decl(inferenceFromIncompleteSource.ts, 0, 0))
6+
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 2, 20))
7+
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 2, 22))
8+
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 2, 20))
9+
10+
items: T[];
11+
>items : Symbol(ListProps.items, Decl(inferenceFromIncompleteSource.ts, 2, 43))
12+
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 2, 20))
13+
14+
itemKey: K;
15+
>itemKey : Symbol(ListProps.itemKey, Decl(inferenceFromIncompleteSource.ts, 3, 13))
16+
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 2, 22))
17+
18+
prop: number;
19+
>prop : Symbol(ListProps.prop, Decl(inferenceFromIncompleteSource.ts, 4, 13))
20+
}
21+
22+
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
23+
>Component : Symbol(Component, Decl(inferenceFromIncompleteSource.ts, 8, 13))
24+
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 8, 26))
25+
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 8, 28))
26+
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 8, 26))
27+
>x : Symbol(x, Decl(inferenceFromIncompleteSource.ts, 8, 48))
28+
>ListProps : Symbol(ListProps, Decl(inferenceFromIncompleteSource.ts, 0, 0))
29+
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 8, 26))
30+
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 8, 28))
31+
32+
Component({items: [{name:' string'}], itemKey: 'name' });
33+
>Component : Symbol(Component, Decl(inferenceFromIncompleteSource.ts, 8, 13))
34+
>items : Symbol(items, Decl(inferenceFromIncompleteSource.ts, 10, 11))
35+
>name : Symbol(name, Decl(inferenceFromIncompleteSource.ts, 10, 20))
36+
>itemKey : Symbol(itemKey, Decl(inferenceFromIncompleteSource.ts, 10, 37))
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/inferenceFromIncompleteSource.ts ===
2+
// Repro from #42030
3+
4+
interface ListProps<T, K extends keyof T> {
5+
items: T[];
6+
>items : T[]
7+
8+
itemKey: K;
9+
>itemKey : K
10+
11+
prop: number;
12+
>prop : number
13+
}
14+
15+
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
16+
>Component : <T, K extends keyof T>(x: ListProps<T, K>) => void
17+
>x : ListProps<T, K>
18+
19+
Component({items: [{name:' string'}], itemKey: 'name' });
20+
>Component({items: [{name:' string'}], itemKey: 'name' }) : void
21+
>Component : <T, K extends keyof T>(x: ListProps<T, K>) => void
22+
>{items: [{name:' string'}], itemKey: 'name' } : { items: { name: string; }[]; itemKey: "name"; }
23+
>items : { name: string; }[]
24+
>[{name:' string'}] : { name: string; }[]
25+
>{name:' string'} : { name: string; }
26+
>name : string
27+
>' string' : " string"
28+
>itemKey : "name"
29+
>'name' : "name"
30+

tests/baselines/reference/tsxStatelessFunctionComponentsWithTypeArguments4.errors.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ tests/cases/conformance/jsx/file.tsx(9,14): error TS2769: No overload matches th
99
tests/cases/conformance/jsx/file.tsx(10,15): error TS2769: No overload matches this call.
1010
Overload 1 of 3, '(): Element', gave the following error.
1111
Type 'T & { "ignore-prop": true; }' has no properties in common with type 'IntrinsicAttributes'.
12-
Overload 2 of 3, '(attr: { b: unknown; a: string; "ignore-prop": boolean; }): Element', gave the following error.
13-
Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: string; "ignore-prop": boolean; }'.
14-
Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: string; "ignore-prop": boolean; }'.
12+
Overload 2 of 3, '(attr: { b: number; a: string; "ignore-prop": boolean; }): Element', gave the following error.
13+
Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: number; a: string; "ignore-prop": boolean; }'.
14+
Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: number; a: string; "ignore-prop": boolean; }'.
1515
Overload 3 of 3, '(attr: { b: unknown; a: unknown; }): Element', gave the following error.
1616
Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: unknown; }'.
1717
Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: unknown; }'.
@@ -43,9 +43,9 @@ tests/cases/conformance/jsx/file.tsx(10,15): error TS2769: No overload matches t
4343
!!! error TS2769: No overload matches this call.
4444
!!! error TS2769: Overload 1 of 3, '(): Element', gave the following error.
4545
!!! error TS2769: Type 'T & { "ignore-prop": true; }' has no properties in common with type 'IntrinsicAttributes'.
46-
!!! error TS2769: Overload 2 of 3, '(attr: { b: unknown; a: string; "ignore-prop": boolean; }): Element', gave the following error.
47-
!!! error TS2769: Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: string; "ignore-prop": boolean; }'.
48-
!!! error TS2769: Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: string; "ignore-prop": boolean; }'.
46+
!!! error TS2769: Overload 2 of 3, '(attr: { b: number; a: string; "ignore-prop": boolean; }): Element', gave the following error.
47+
!!! error TS2769: Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: number; a: string; "ignore-prop": boolean; }'.
48+
!!! error TS2769: Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: number; a: string; "ignore-prop": boolean; }'.
4949
!!! error TS2769: Overload 3 of 3, '(attr: { b: unknown; a: unknown; }): Element', gave the following error.
5050
!!! error TS2769: Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: unknown; }'.
5151
!!! error TS2769: Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: unknown; }'.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @strict: true
2+
3+
// Repro from #42030
4+
5+
interface ListProps<T, K extends keyof T> {
6+
items: T[];
7+
itemKey: K;
8+
prop: number;
9+
}
10+
11+
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
12+
13+
Component({items: [{name:' string'}], itemKey: 'name' });

0 commit comments

Comments
 (0)