mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Properly make inferences from partial source type (#42038)
* Slightly less picky check in typesDefinitelyUnrelated * Accept new baselines * Add regression test
This commit is contained in:
@@ -20045,7 +20045,7 @@ namespace ts {
|
||||
// Two object types that each have a property that is unmatched in the other are definitely unrelated.
|
||||
return isTupleType(source) && isTupleType(target) ? tupleTypesDefinitelyUnrelated(source, target) :
|
||||
!!getUnmatchedProperty(source, target, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true) &&
|
||||
!!getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ true);
|
||||
!!getUnmatchedProperty(target, source, /*requireOptionalProperties*/ false, /*matchDiscriminantProperties*/ false);
|
||||
}
|
||||
|
||||
function getTypeFromInference(inference: InferenceInfo) {
|
||||
|
||||
@@ -23,8 +23,8 @@ tests/cases/conformance/types/rest/genericRestParameters3.ts(37,21): error TS234
|
||||
Property '0' is missing in type 'CoolArray<any>' but required in type '[cb: (...args: any[]) => void]'.
|
||||
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>'.
|
||||
Property 'hello' is missing in type '[10, 20]' but required in type 'CoolArray<number>'.
|
||||
tests/cases/conformance/types/rest/genericRestParameters3.ts(49,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<unknown>'.
|
||||
Property 'hello' is missing in type '[]' but required in type 'CoolArray<unknown>'.
|
||||
tests/cases/conformance/types/rest/genericRestParameters3.ts(49,1): error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
|
||||
Property 'hello' is missing in type '[]' but required in type 'CoolArray<never>'.
|
||||
tests/cases/conformance/types/rest/genericRestParameters3.ts(50,5): error TS2345: Argument of type '[number]' is not assignable to parameter of type 'CoolArray<unknown>'.
|
||||
Property 'hello' is missing in type '[number]' but required in type 'CoolArray<unknown>'.
|
||||
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
|
||||
|
||||
baz(); // Error
|
||||
~~~~~
|
||||
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<unknown>'.
|
||||
!!! error TS2345: Property 'hello' is missing in type '[]' but required in type 'CoolArray<unknown>'.
|
||||
!!! error TS2345: Argument of type '[]' is not assignable to parameter of type 'CoolArray<never>'.
|
||||
!!! error TS2345: Property 'hello' is missing in type '[]' but required in type 'CoolArray<never>'.
|
||||
!!! related TS2728 tests/cases/conformance/types/rest/genericRestParameters3.ts:30:5: 'hello' is declared here.
|
||||
baz(1); // Error
|
||||
~
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
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">'.
|
||||
Property 'prop' is missing in type '{ items: { name: string; }[]; itemKey: "name"; }' but required in type 'ListProps<{ name: string; }, "name">'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/inferenceFromIncompleteSource.ts (1 errors) ====
|
||||
// Repro from #42030
|
||||
|
||||
interface ListProps<T, K extends keyof T> {
|
||||
items: T[];
|
||||
itemKey: K;
|
||||
prop: number;
|
||||
}
|
||||
|
||||
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
|
||||
|
||||
Component({items: [{name:' string'}], itemKey: 'name' });
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2345: Argument of type '{ items: { name: string; }[]; itemKey: "name"; }' is not assignable to parameter of type 'ListProps<{ name: string; }, "name">'.
|
||||
!!! error TS2345: Property 'prop' is missing in type '{ items: { name: string; }[]; itemKey: "name"; }' but required in type 'ListProps<{ name: string; }, "name">'.
|
||||
!!! related TS2728 tests/cases/compiler/inferenceFromIncompleteSource.ts:6:3: 'prop' is declared here.
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
//// [inferenceFromIncompleteSource.ts]
|
||||
// Repro from #42030
|
||||
|
||||
interface ListProps<T, K extends keyof T> {
|
||||
items: T[];
|
||||
itemKey: K;
|
||||
prop: number;
|
||||
}
|
||||
|
||||
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
|
||||
|
||||
Component({items: [{name:' string'}], itemKey: 'name' });
|
||||
|
||||
|
||||
//// [inferenceFromIncompleteSource.js]
|
||||
"use strict";
|
||||
// Repro from #42030
|
||||
Component({ items: [{ name: ' string' }], itemKey: 'name' });
|
||||
@@ -0,0 +1,37 @@
|
||||
=== tests/cases/compiler/inferenceFromIncompleteSource.ts ===
|
||||
// Repro from #42030
|
||||
|
||||
interface ListProps<T, K extends keyof T> {
|
||||
>ListProps : Symbol(ListProps, Decl(inferenceFromIncompleteSource.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 2, 20))
|
||||
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 2, 22))
|
||||
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 2, 20))
|
||||
|
||||
items: T[];
|
||||
>items : Symbol(ListProps.items, Decl(inferenceFromIncompleteSource.ts, 2, 43))
|
||||
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 2, 20))
|
||||
|
||||
itemKey: K;
|
||||
>itemKey : Symbol(ListProps.itemKey, Decl(inferenceFromIncompleteSource.ts, 3, 13))
|
||||
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 2, 22))
|
||||
|
||||
prop: number;
|
||||
>prop : Symbol(ListProps.prop, Decl(inferenceFromIncompleteSource.ts, 4, 13))
|
||||
}
|
||||
|
||||
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
|
||||
>Component : Symbol(Component, Decl(inferenceFromIncompleteSource.ts, 8, 13))
|
||||
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 8, 26))
|
||||
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 8, 28))
|
||||
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 8, 26))
|
||||
>x : Symbol(x, Decl(inferenceFromIncompleteSource.ts, 8, 48))
|
||||
>ListProps : Symbol(ListProps, Decl(inferenceFromIncompleteSource.ts, 0, 0))
|
||||
>T : Symbol(T, Decl(inferenceFromIncompleteSource.ts, 8, 26))
|
||||
>K : Symbol(K, Decl(inferenceFromIncompleteSource.ts, 8, 28))
|
||||
|
||||
Component({items: [{name:' string'}], itemKey: 'name' });
|
||||
>Component : Symbol(Component, Decl(inferenceFromIncompleteSource.ts, 8, 13))
|
||||
>items : Symbol(items, Decl(inferenceFromIncompleteSource.ts, 10, 11))
|
||||
>name : Symbol(name, Decl(inferenceFromIncompleteSource.ts, 10, 20))
|
||||
>itemKey : Symbol(itemKey, Decl(inferenceFromIncompleteSource.ts, 10, 37))
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
=== tests/cases/compiler/inferenceFromIncompleteSource.ts ===
|
||||
// Repro from #42030
|
||||
|
||||
interface ListProps<T, K extends keyof T> {
|
||||
items: T[];
|
||||
>items : T[]
|
||||
|
||||
itemKey: K;
|
||||
>itemKey : K
|
||||
|
||||
prop: number;
|
||||
>prop : number
|
||||
}
|
||||
|
||||
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
|
||||
>Component : <T, K extends keyof T>(x: ListProps<T, K>) => void
|
||||
>x : ListProps<T, K>
|
||||
|
||||
Component({items: [{name:' string'}], itemKey: 'name' });
|
||||
>Component({items: [{name:' string'}], itemKey: 'name' }) : void
|
||||
>Component : <T, K extends keyof T>(x: ListProps<T, K>) => void
|
||||
>{items: [{name:' string'}], itemKey: 'name' } : { items: { name: string; }[]; itemKey: "name"; }
|
||||
>items : { name: string; }[]
|
||||
>[{name:' string'}] : { name: string; }[]
|
||||
>{name:' string'} : { name: string; }
|
||||
>name : string
|
||||
>' string' : " string"
|
||||
>itemKey : "name"
|
||||
>'name' : "name"
|
||||
|
||||
+6
-6
@@ -9,9 +9,9 @@ tests/cases/conformance/jsx/file.tsx(9,14): error TS2769: No overload matches th
|
||||
tests/cases/conformance/jsx/file.tsx(10,15): error TS2769: No overload matches this call.
|
||||
Overload 1 of 3, '(): Element', gave the following error.
|
||||
Type 'T & { "ignore-prop": true; }' has no properties in common with type 'IntrinsicAttributes'.
|
||||
Overload 2 of 3, '(attr: { b: unknown; a: string; "ignore-prop": boolean; }): Element', gave the following error.
|
||||
Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: string; "ignore-prop": boolean; }'.
|
||||
Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: string; "ignore-prop": boolean; }'.
|
||||
Overload 2 of 3, '(attr: { b: number; a: string; "ignore-prop": boolean; }): Element', gave the following error.
|
||||
Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: number; a: string; "ignore-prop": boolean; }'.
|
||||
Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: number; a: string; "ignore-prop": boolean; }'.
|
||||
Overload 3 of 3, '(attr: { b: unknown; a: unknown; }): Element', gave the following error.
|
||||
Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: unknown; }'.
|
||||
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
|
||||
!!! error TS2769: No overload matches this call.
|
||||
!!! error TS2769: Overload 1 of 3, '(): Element', gave the following error.
|
||||
!!! error TS2769: Type 'T & { "ignore-prop": true; }' has no properties in common with type 'IntrinsicAttributes'.
|
||||
!!! error TS2769: Overload 2 of 3, '(attr: { b: unknown; a: string; "ignore-prop": boolean; }): Element', gave the following error.
|
||||
!!! error TS2769: Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: string; "ignore-prop": boolean; }'.
|
||||
!!! error TS2769: Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: string; "ignore-prop": boolean; }'.
|
||||
!!! error TS2769: Overload 2 of 3, '(attr: { b: number; a: string; "ignore-prop": boolean; }): Element', gave the following error.
|
||||
!!! error TS2769: Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: number; a: string; "ignore-prop": boolean; }'.
|
||||
!!! error TS2769: Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: number; a: string; "ignore-prop": boolean; }'.
|
||||
!!! error TS2769: Overload 3 of 3, '(attr: { b: unknown; a: unknown; }): Element', gave the following error.
|
||||
!!! error TS2769: Type 'T & { "ignore-prop": true; }' is not assignable to type 'IntrinsicAttributes & { b: unknown; a: unknown; }'.
|
||||
!!! error TS2769: Property 'a' is missing in type '{ b: number; } & { "ignore-prop": true; }' but required in type '{ b: unknown; a: unknown; }'.
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
// @strict: true
|
||||
|
||||
// Repro from #42030
|
||||
|
||||
interface ListProps<T, K extends keyof T> {
|
||||
items: T[];
|
||||
itemKey: K;
|
||||
prop: number;
|
||||
}
|
||||
|
||||
declare const Component: <T, K extends keyof T>(x: ListProps<T, K>) => void;
|
||||
|
||||
Component({items: [{name:' string'}], itemKey: 'name' });
|
||||
Reference in New Issue
Block a user