Skip to content

Commit f86b635

Browse files
authored
Merge pull request microsoft#29705 from Microsoft/fixParametersAndReturnType
Less restrictive Parameters<T> and ReturnType<T>
2 parents 530a09a + 118a2cc commit f86b635

9 files changed

Lines changed: 523 additions & 494 deletions

File tree

src/lib/es5.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,22 +1451,22 @@ type NonNullable<T> = T extends null | undefined ? never : T;
14511451
/**
14521452
* Obtain the parameters of a function type in a tuple
14531453
*/
1454-
type Parameters<T extends (...args: any[]) => any> = T extends (...args: infer P) => any ? P : never;
1454+
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
14551455

14561456
/**
14571457
* Obtain the parameters of a constructor function type in a tuple
14581458
*/
1459-
type ConstructorParameters<T extends new (...args: any[]) => any> = T extends new (...args: infer P) => any ? P : never;
1459+
type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
14601460

14611461
/**
14621462
* Obtain the return type of a function type
14631463
*/
1464-
type ReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : any;
1464+
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
14651465

14661466
/**
14671467
* Obtain the return type of a constructor function type
14681468
*/
1469-
type InstanceType<T extends new (...args: any[]) => any> = T extends new (...args: any[]) => infer R ? R : any;
1469+
type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;
14701470

14711471
/**
14721472
* Marker for contextual 'this' type

tests/baselines/reference/genericRestParameters1.errors.txt

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
11
tests/cases/conformance/types/rest/genericRestParameters1.ts(22,1): error TS2556: Expected 3 arguments, but got 1 or more.
22
tests/cases/conformance/types/rest/genericRestParameters1.ts(31,1): error TS2556: Expected 3 arguments, but got 1 or more.
3-
tests/cases/conformance/types/rest/genericRestParameters1.ts(133,40): error TS2344: Type '(...args: T) => void' does not satisfy the constraint '(...args: any[]) => any'.
4-
Types of parameters 'args' and 'args' are incompatible.
5-
Type 'any[]' is not assignable to type 'T'.
6-
tests/cases/conformance/types/rest/genericRestParameters1.ts(134,51): error TS2344: Type 'new (...args: T) => void' does not satisfy the constraint 'new (...args: any[]) => any'.
7-
Types of parameters 'args' and 'args' are incompatible.
8-
Type 'any[]' is not assignable to type 'T'.
9-
tests/cases/conformance/types/rest/genericRestParameters1.ts(135,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: any[]) => any'.
10-
Type 'Function' provides no match for the signature '(...args: any[]): any'.
3+
tests/cases/conformance/types/rest/genericRestParameters1.ts(135,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
4+
Type 'Function' provides no match for the signature '(...args: any): any'.
115
tests/cases/conformance/types/rest/genericRestParameters1.ts(164,1): error TS2322: Type '(a: never) => void' is not assignable to type '(...args: any[]) => void'.
126
Types of parameters 'a' and 'args' are incompatible.
137
Type 'any' is not assignable to type 'never'.
148

159

16-
==== tests/cases/conformance/types/rest/genericRestParameters1.ts (6 errors) ====
10+
==== tests/cases/conformance/types/rest/genericRestParameters1.ts (4 errors) ====
1711
declare let f1: (...x: [number, string, boolean]) => void;
1812
declare let f2: (x0: number, x1: string, x2: boolean) => void;
1913

@@ -152,19 +146,11 @@ tests/cases/conformance/types/rest/genericRestParameters1.ts(164,1): error TS232
152146
type T05<T> = Parameters<(...args: T[]) => void>;
153147
type T06<T> = ConstructorParameters<new (...args: []) => void>;
154148
type T07<T extends any[]> = Parameters<(...args: T) => void>;
155-
~~~~~~~~~~~~~~~~~~~~
156-
!!! error TS2344: Type '(...args: T) => void' does not satisfy the constraint '(...args: any[]) => any'.
157-
!!! error TS2344: Types of parameters 'args' and 'args' are incompatible.
158-
!!! error TS2344: Type 'any[]' is not assignable to type 'T'.
159149
type T08<T extends any[]> = ConstructorParameters<new (...args: T) => void>;
160-
~~~~~~~~~~~~~~~~~~~~~~~~
161-
!!! error TS2344: Type 'new (...args: T) => void' does not satisfy the constraint 'new (...args: any[]) => any'.
162-
!!! error TS2344: Types of parameters 'args' and 'args' are incompatible.
163-
!!! error TS2344: Type 'any[]' is not assignable to type 'T'.
164150
type T09 = Parameters<Function>;
165151
~~~~~~~~
166-
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: any[]) => any'.
167-
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: any[]): any'.
152+
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
153+
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: any): any'.
168154

169155
type Record1 = {
170156
move: [number, 'left' | 'right'];

tests/baselines/reference/genericRestParameters2.errors.txt

Lines changed: 0 additions & 89 deletions
This file was deleted.

tests/baselines/reference/inferTypes1.errors.txt

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
tests/cases/conformance/types/conditional/inferTypes1.ts(31,23): error TS2344: Type 'string' does not satisfy the constraint '(...args: any[]) => any'.
2-
tests/cases/conformance/types/conditional/inferTypes1.ts(32,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: any[]) => any'.
3-
Type 'Function' provides no match for the signature '(...args: any[]): any'.
4-
tests/cases/conformance/types/conditional/inferTypes1.ts(37,25): error TS2344: Type 'string' does not satisfy the constraint 'new (...args: any[]) => any'.
5-
tests/cases/conformance/types/conditional/inferTypes1.ts(38,25): error TS2344: Type 'Function' does not satisfy the constraint 'new (...args: any[]) => any'.
6-
Type 'Function' provides no match for the signature 'new (...args: any[]): any'.
7-
tests/cases/conformance/types/conditional/inferTypes1.ts(46,25): error TS2344: Type '(x: string, y: string) => number' does not satisfy the constraint '(x: any) => any'.
8-
tests/cases/conformance/types/conditional/inferTypes1.ts(47,25): error TS2344: Type 'Function' does not satisfy the constraint '(x: any) => any'.
1+
tests/cases/conformance/types/conditional/inferTypes1.ts(31,23): error TS2344: Type 'string' does not satisfy the constraint '(...args: any) => any'.
2+
tests/cases/conformance/types/conditional/inferTypes1.ts(32,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
3+
Type 'Function' provides no match for the signature '(...args: any): any'.
4+
tests/cases/conformance/types/conditional/inferTypes1.ts(38,25): error TS2344: Type 'string' does not satisfy the constraint 'new (...args: any) => any'.
5+
tests/cases/conformance/types/conditional/inferTypes1.ts(39,25): error TS2344: Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
6+
Type 'Function' provides no match for the signature 'new (...args: any): any'.
7+
tests/cases/conformance/types/conditional/inferTypes1.ts(47,25): error TS2344: Type '(x: string, y: string) => number' does not satisfy the constraint '(x: any) => any'.
8+
tests/cases/conformance/types/conditional/inferTypes1.ts(48,25): error TS2344: Type 'Function' does not satisfy the constraint '(x: any) => any'.
99
Type 'Function' provides no match for the signature '(x: any): any'.
10-
tests/cases/conformance/types/conditional/inferTypes1.ts(73,12): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
11-
tests/cases/conformance/types/conditional/inferTypes1.ts(74,15): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
12-
tests/cases/conformance/types/conditional/inferTypes1.ts(74,41): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
13-
tests/cases/conformance/types/conditional/inferTypes1.ts(74,51): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
14-
tests/cases/conformance/types/conditional/inferTypes1.ts(75,15): error TS2304: Cannot find name 'U'.
15-
tests/cases/conformance/types/conditional/inferTypes1.ts(75,15): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
16-
tests/cases/conformance/types/conditional/inferTypes1.ts(75,43): error TS2304: Cannot find name 'U'.
17-
tests/cases/conformance/types/conditional/inferTypes1.ts(75,43): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
18-
tests/cases/conformance/types/conditional/inferTypes1.ts(82,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
10+
tests/cases/conformance/types/conditional/inferTypes1.ts(74,12): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
11+
tests/cases/conformance/types/conditional/inferTypes1.ts(75,15): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
12+
tests/cases/conformance/types/conditional/inferTypes1.ts(75,41): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
13+
tests/cases/conformance/types/conditional/inferTypes1.ts(75,51): error TS1338: 'infer' declarations are only permitted in the 'extends' clause of a conditional type.
14+
tests/cases/conformance/types/conditional/inferTypes1.ts(76,15): error TS2304: Cannot find name 'U'.
15+
tests/cases/conformance/types/conditional/inferTypes1.ts(76,15): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
16+
tests/cases/conformance/types/conditional/inferTypes1.ts(76,43): error TS2304: Cannot find name 'U'.
17+
tests/cases/conformance/types/conditional/inferTypes1.ts(76,43): error TS4081: Exported type alias 'T62' has or is using private name 'U'.
18+
tests/cases/conformance/types/conditional/inferTypes1.ts(83,44): error TS2344: Type 'U' does not satisfy the constraint 'string'.
1919
Type 'number' is not assignable to type 'string'.
20-
tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
20+
tests/cases/conformance/types/conditional/inferTypes1.ts(145,40): error TS2322: Type 'T' is not assignable to type 'string | number | symbol'.
2121
Type 'T' is not assignable to type 'symbol'.
2222

2323

@@ -54,22 +54,23 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322:
5454
type T16 = ReturnType<never>; // never
5555
type T17 = ReturnType<string>; // Error
5656
~~~~~~
57-
!!! error TS2344: Type 'string' does not satisfy the constraint '(...args: any[]) => any'.
57+
!!! error TS2344: Type 'string' does not satisfy the constraint '(...args: any) => any'.
5858
type T18 = ReturnType<Function>; // Error
5959
~~~~~~~~
60-
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: any[]) => any'.
61-
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: any[]): any'.
60+
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
61+
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: any): any'.
62+
type T19<T extends any[]> = ReturnType<(x: string, ...args: T) => T[]>; // T[]
6263

6364
type U10 = InstanceType<typeof C>; // C
6465
type U11 = InstanceType<any>; // any
6566
type U12 = InstanceType<never>; // never
6667
type U13 = InstanceType<string>; // Error
6768
~~~~~~
68-
!!! error TS2344: Type 'string' does not satisfy the constraint 'new (...args: any[]) => any'.
69+
!!! error TS2344: Type 'string' does not satisfy the constraint 'new (...args: any) => any'.
6970
type U14 = InstanceType<Function>; // Error
7071
~~~~~~~~
71-
!!! error TS2344: Type 'Function' does not satisfy the constraint 'new (...args: any[]) => any'.
72-
!!! error TS2344: Type 'Function' provides no match for the signature 'new (...args: any[]): any'.
72+
!!! error TS2344: Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
73+
!!! error TS2344: Type 'Function' provides no match for the signature 'new (...args: any): any'.
7374

7475
type ArgumentType<T extends (x: any) => any> = T extends (a: infer A) => any ? A : any;
7576

@@ -226,4 +227,14 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(144,40): error TS2322:
226227

227228
type Test1 = EnsureIsString<"hello">; // "hello"
228229
type Test2 = EnsureIsString<42>; // never
230+
231+
// Repros from #26856
232+
233+
function invoker <K extends string | number | symbol, A extends any[]> (key: K, ...args: A) {
234+
return <T extends Record<K, (...args: A) => any>> (obj: T): ReturnType<T[K]> => obj[key](...args)
235+
}
236+
237+
const result = invoker('test', true)({ test: (a: boolean) => 123 })
238+
239+
type Foo2<A extends any[]> = ReturnType<(...args: A) => string>;
229240

tests/baselines/reference/inferTypes1.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type T15 = ReturnType<any>; // any
3131
type T16 = ReturnType<never>; // never
3232
type T17 = ReturnType<string>; // Error
3333
type T18 = ReturnType<Function>; // Error
34+
type T19<T extends any[]> = ReturnType<(x: string, ...args: T) => T[]>; // T[]
3435

3536
type U10 = InstanceType<typeof C>; // C
3637
type U11 = InstanceType<any>; // any
@@ -166,6 +167,16 @@ type EnsureIsString<T> = T extends MustBeString<infer U> ? U : never;
166167

167168
type Test1 = EnsureIsString<"hello">; // "hello"
168169
type Test2 = EnsureIsString<42>; // never
170+
171+
// Repros from #26856
172+
173+
function invoker <K extends string | number | symbol, A extends any[]> (key: K, ...args: A) {
174+
return <T extends Record<K, (...args: A) => any>> (obj: T): ReturnType<T[K]> => obj[key](...args)
175+
}
176+
177+
const result = invoker('test', true)({ test: (a: boolean) => 123 })
178+
179+
type Foo2<A extends any[]> = ReturnType<(...args: A) => string>;
169180

170181

171182
//// [inferTypes1.js]
@@ -182,3 +193,12 @@ var C = /** @class */ (function () {
182193
}());
183194
var z1 = ex.customClass;
184195
var z2 = ex.obj.nested.attr;
196+
// Repros from #26856
197+
function invoker(key) {
198+
var args = [];
199+
for (var _i = 1; _i < arguments.length; _i++) {
200+
args[_i - 1] = arguments[_i];
201+
}
202+
return function (obj) { return obj[key].apply(obj, args); };
203+
}
204+
var result = invoker('test', true)({ test: function (a) { return 123; } });

0 commit comments

Comments
 (0)