Skip to content

Better Inference for mapped array and tuple types #50046

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 129 additions & 34 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1196,6 +1196,7 @@ export namespace Completion {
typeEntry("Record"),
typeEntry("Exclude"),
typeEntry("Extract"),
typeEntry("Cast"),
typeEntry("Omit"),
typeEntry("NonNullable"),
typeEntry("Parameters"),
Expand Down
13 changes: 9 additions & 4 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1200,7 +1200,7 @@ interface ReadonlyArray<T> {
* @param thisArg An object to which the this keyword can refer in the predicate function.
* If thisArg is omitted, undefined is used as the this value.
*/
every<S extends T>(predicate: (value: T, index: number, array: readonly T[]) => value is S, thisArg?: any): this is readonly S[];
every<S extends T>(predicate: (value: T, index: number, array: readonly T[]) => value is S, thisArg?: any): this is Cast<{ [P in keyof this]: S }, readonly S[]>;
/**
* Determines whether all the members of an array satisfy the specified test.
* @param predicate A function that accepts up to three arguments. The every method calls
Expand Down Expand Up @@ -1230,7 +1230,7 @@ interface ReadonlyArray<T> {
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
map<U>(callbackfn: (value: T, index: number, array: readonly T[]) => U, thisArg?: any): U[];
map<U>(callbackfn: (value: T, index: number, array: readonly T[]) => U, thisArg?: any): Cast<{ -readonly [P in keyof this]: U }, U[]>;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
Expand Down Expand Up @@ -1391,7 +1391,7 @@ interface Array<T> {
* @param thisArg An object to which the this keyword can refer in the predicate function.
* If thisArg is omitted, undefined is used as the this value.
*/
every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[];
every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is Cast<{ [P in keyof this]: S }, S[]>;
/**
* Determines whether all the members of an array satisfy the specified test.
* @param predicate A function that accepts up to three arguments. The every method calls
Expand Down Expand Up @@ -1421,7 +1421,7 @@ interface Array<T> {
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[];
map<U>(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): Cast<{ [P in keyof this]: U }, U[]>;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array.
Expand Down Expand Up @@ -1579,6 +1579,11 @@ type Exclude<T, U> = T extends U ? never : T;
*/
type Extract<T, U> = T extends U ? T : never;

/**
* Make sure T is at least assignable to U
*/
type Cast<T, U> = T extends U ? T : U;

/**
* Construct a type with the properties of T except for those in type K.
*/
Expand Down
5 changes: 2 additions & 3 deletions tests/baselines/reference/arrayDestructuringInSwitch1.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export function evaluate(expression: Expression): boolean {

case 'and': {
return operands.every((child) => evaluate(child));
>operands.every : Symbol(Array.every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>operands.every : Symbol(Array.every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>operands : Symbol(operands, Decl(arrayDestructuringInSwitch1.ts, 5, 20))
>every : Symbol(Array.every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>every : Symbol(Array.every, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>child : Symbol(child, Decl(arrayDestructuringInSwitch1.ts, 8, 31))
>evaluate : Symbol(evaluate, Decl(arrayDestructuringInSwitch1.ts, 1, 84))
>child : Symbol(child, Decl(arrayDestructuringInSwitch1.ts, 8, 31))
Expand All @@ -40,7 +40,6 @@ export function evaluate(expression: Expression): boolean {
return !evaluate(operands[0]);
>evaluate : Symbol(evaluate, Decl(arrayDestructuringInSwitch1.ts, 1, 84))
>operands : Symbol(operands, Decl(arrayDestructuringInSwitch1.ts, 5, 20))
>0 : Symbol(0)
}
default: {
throw new Error(`${operator} is not a supported operator`);
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/arrayDestructuringInSwitch1.types
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function evaluate(expression: Expression): boolean {

const [operator, ...operands] = expression;
>operator : "and" | "not"
>operands : Expression[] | [Expression]
>operands : Expression[]
>expression : BooleanLogicExpression

switch (operator) {
Expand All @@ -29,9 +29,9 @@ export function evaluate(expression: Expression): boolean {

return operands.every((child) => evaluate(child));
>operands.every((child) => evaluate(child)) : boolean
>operands.every : { <S extends Expression>(predicate: (value: Expression, index: number, array: Expression[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Expression, index: number, array: Expression[]) => unknown, thisArg?: any): boolean; } | { <S extends Expression>(predicate: (value: Expression, index: number, array: Expression[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Expression, index: number, array: Expression[]) => unknown, thisArg?: any): boolean; }
>operands : Expression[] | [Expression]
>every : { <S extends Expression>(predicate: (value: Expression, index: number, array: Expression[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Expression, index: number, array: Expression[]) => unknown, thisArg?: any): boolean; } | { <S extends Expression>(predicate: (value: Expression, index: number, array: Expression[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Expression, index: number, array: Expression[]) => unknown, thisArg?: any): boolean; }
>operands.every : { <S extends Expression>(predicate: (value: Expression, index: number, array: Expression[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Expression, index: number, array: Expression[]) => unknown, thisArg?: any): boolean; }
>operands : Expression[]
>every : { <S extends Expression>(predicate: (value: Expression, index: number, array: Expression[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Expression, index: number, array: Expression[]) => unknown, thisArg?: any): boolean; }
>(child) => evaluate(child) : (child: Expression) => boolean
>child : Expression
>evaluate(child) : boolean
Expand All @@ -46,7 +46,7 @@ export function evaluate(expression: Expression): boolean {
>evaluate(operands[0]) : boolean
>evaluate : (expression: Expression) => boolean
>operands[0] : Expression
>operands : Expression[] | [Expression]
>operands : Expression[]
>0 : 0
}
default: {
Expand Down
22 changes: 11 additions & 11 deletions tests/baselines/reference/bestChoiceType.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Repro from #10041

(''.match(/ /) || []).map(s => s.toLowerCase());
>(''.match(/ /) || []).map(s => s.toLowerCase()) : any[]
>(''.match(/ /) || []).map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>(''.match(/ /) || []).map(s => s.toLowerCase()) : [] | (any[] & { 0: U; })
>(''.match(/ /) || []).map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[] & { 0: U; }) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => [])
>(''.match(/ /) || []) : RegExpMatchArray | []
>''.match(/ /) || [] : RegExpMatchArray | []
>''.match(/ /) : RegExpMatchArray | null
Expand All @@ -12,7 +12,7 @@
>match : (regexp: string | RegExp) => RegExpMatchArray | null
>/ / : RegExp
>[] : []
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[] & { 0: U; }) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => [])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
Expand Down Expand Up @@ -40,11 +40,11 @@ function f1() {
>[] : []

let z = y.map(s => s.toLowerCase());
>z : any[]
>y.map(s => s.toLowerCase()) : any[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>z : [] | (any[] & { 0: U; })
>y.map(s => s.toLowerCase()) : [] | (any[] & { 0: U; })
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[] & { 0: U; }) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => [])
>y : RegExpMatchArray | []
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[] & { 0: U; }) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => [])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
Expand Down Expand Up @@ -72,11 +72,11 @@ function f2() {
>[] : never[]

let z = y.map(s => s.toLowerCase());
>z : any[]
>y.map(s => s.toLowerCase()) : any[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>z : (any[] & { 0: U; }) | never[]
>y.map(s => s.toLowerCase()) : (any[] & { 0: U; }) | never[]
>y.map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[] & { 0: U; }) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => never[])
>y : RegExpMatchArray | never[]
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => U[])
>map : (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[] & { 0: U; }) | (<U>(callbackfn: (value: never, index: number, array: never[]) => U, thisArg?: any) => never[])
>s => s.toLowerCase() : (s: any) => any
>s : any
>s.toLowerCase() : any
Expand Down
6 changes: 1 addition & 5 deletions tests/baselines/reference/circularBaseTypes.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
tests/cases/compiler/circularBaseTypes.ts(4,11): error TS2310: Type 'M2' recursively references itself as a base type.
tests/cases/compiler/circularBaseTypes.ts(5,6): error TS2456: Type alias 'M3' circularly references itself.
tests/cases/compiler/circularBaseTypes.ts(13,21): error TS2313: Type parameter 'K' has a circular constraint.
tests/cases/compiler/circularBaseTypes.ts(14,11): error TS2310: Type 'Y' recursively references itself as a base type.


==== tests/cases/compiler/circularBaseTypes.ts (4 errors) ====
==== tests/cases/compiler/circularBaseTypes.ts (3 errors) ====
// Repro from #38098

type M<T> = { value: T };
Expand All @@ -22,9 +21,6 @@ tests/cases/compiler/circularBaseTypes.ts(14,11): error TS2310: Type 'Y' recursi
// Repro from #32581

type X<T> = { [K in keyof T]: string } & { b: string };
~~~~~~~
!!! error TS2313: Type parameter 'K' has a circular constraint.
!!! related TS2751 tests/cases/compiler/circularBaseTypes.ts:14:11: Circularity originates in type at this location.
interface Y extends X<Y> {
~
!!! error TS2310: Type 'Y' recursively references itself as a base type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ type FooBar = { foo: "hello"; bar: "world"; };
>bar : "world"

type WithIndex = Compute<FooBar & IndexObject>; // { [x: string]: {}; foo: "hello"; bar: "world"; } <-- OK
>WithIndex : { [x: string]: {}; foo: "hello"; bar: "world"; }
>WithIndex : { foo: "hello"; bar: "world"; } & { [x: string]: {}; }

type WithoutIndex = OmitIndex<WithIndex, string>; // { foo: "hello"; bar: "world"; } <-- OK
>WithoutIndex : OmitIndex<{ [x: string]: {}; foo: "hello"; bar: "world"; }, string>
>WithoutIndex : OmitIndex<{ foo: "hello"; bar: "world"; }, string> & OmitIndex<{ [x: string]: {}; }, string>

type FooBarKey = keyof FooBar; // "foo" | "bar" <-- OK
>FooBarKey : keyof FooBar
Expand All @@ -39,5 +39,5 @@ type WithIndexKey = keyof WithIndex; // string | number <-- Expected: stri
>WithIndexKey : string | number

type WithoutIndexKey = keyof WithoutIndex; // number <-- Expected: "foo" | "bar"
>WithoutIndexKey : "foo" | "bar"
>WithoutIndexKey : keyof FooBar

22 changes: 11 additions & 11 deletions tests/baselines/reference/contextuallyTypedIife.types
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@
>(...numbers) => numbers.every(n => n > 0) : (numbers_0: number, numbers_1: number, numbers_2: number) => boolean
>numbers : [number, number, number]
>numbers.every(n => n > 0) : boolean
>numbers.every : { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): boolean; }
>numbers.every : { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): this is [S, S, S]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): boolean; }
>numbers : [number, number, number]
>every : { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): boolean; }
>every : { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): this is [S, S, S]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): boolean; }
>n => n > 0 : (n: number) => boolean
>n : number
>n > 0 : boolean
Expand All @@ -123,9 +123,9 @@
>(...mixed) => mixed.every(n => !!n) : (mixed_0: number, mixed_1: string, mixed_2: string) => boolean
>mixed : [number, string, string]
>mixed.every(n => !!n) : boolean
>mixed.every : { <S extends string | number>(predicate: (value: string | number, index: number, array: (string | number)[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: string | number, index: number, array: (string | number)[]) => unknown, thisArg?: any): boolean; }
>mixed.every : { <S extends string | number>(predicate: (value: string | number, index: number, array: (string | number)[]) => value is S, thisArg?: any): this is [S, S, S]; (predicate: (value: string | number, index: number, array: (string | number)[]) => unknown, thisArg?: any): boolean; }
>mixed : [number, string, string]
>every : { <S extends string | number>(predicate: (value: string | number, index: number, array: (string | number)[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: string | number, index: number, array: (string | number)[]) => unknown, thisArg?: any): boolean; }
>every : { <S extends string | number>(predicate: (value: string | number, index: number, array: (string | number)[]) => value is S, thisArg?: any): this is [S, S, S]; (predicate: (value: string | number, index: number, array: (string | number)[]) => unknown, thisArg?: any): boolean; }
>n => !!n : (n: string | number) => boolean
>n : string | number
>!!n : boolean
Expand All @@ -151,18 +151,18 @@
>0 : 0

((first, ...rest) => first ? [] : rest.map(n => n > 0))(8,9,10);
>((first, ...rest) => first ? [] : rest.map(n => n > 0))(8,9,10) : boolean[]
>((first, ...rest) => first ? [] : rest.map(n => n > 0)) : (first: number, rest_0: number, rest_1: number) => boolean[]
>(first, ...rest) => first ? [] : rest.map(n => n > 0) : (first: number, rest_0: number, rest_1: number) => boolean[]
>((first, ...rest) => first ? [] : rest.map(n => n > 0))(8,9,10) : [] | [boolean, boolean]
>((first, ...rest) => first ? [] : rest.map(n => n > 0)) : (first: number, rest_0: number, rest_1: number) => [] | [boolean, boolean]
>(first, ...rest) => first ? [] : rest.map(n => n > 0) : (first: number, rest_0: number, rest_1: number) => [] | [boolean, boolean]
>first : number
>rest : [number, number]
>first ? [] : rest.map(n => n > 0) : boolean[]
>first ? [] : rest.map(n => n > 0) : [boolean, boolean] | []
>first : number
>[] : undefined[]
>rest.map(n => n > 0) : boolean[]
>rest.map : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]
>rest.map(n => n > 0) : [boolean, boolean]
>rest.map : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => [U, U]
>rest : [number, number]
>map : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]
>map : <U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => [U, U]
>n => n > 0 : (n: number) => boolean
>n : number
>n > 0 : boolean
Expand Down
Loading