Skip to content

Commit 0a2d583

Browse files
committed
Use ...args: never in builtin conditional type helpers
1 parent eb2d1f9 commit 0a2d583

11 files changed

+136
-101
lines changed

src/lib/es5.d.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1603,17 +1603,17 @@ type NonNullable<T> = T & {};
16031603
/**
16041604
* Obtain the parameters of a function type in a tuple
16051605
*/
1606-
type Parameters<T extends (...args: any) => any> = T extends (...args: infer P) => any ? P : never;
1606+
type Parameters<T extends (...args: never) => any> = T extends (...args: infer P) => any ? P : never;
16071607

16081608
/**
16091609
* Obtain the parameters of a constructor function type in a tuple
16101610
*/
1611-
type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;
1611+
type ConstructorParameters<T extends abstract new (...args: never) => any> = T extends abstract new (...args: infer P) => any ? P : never;
16121612

16131613
/**
16141614
* Obtain the return type of a function type
16151615
*/
1616-
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
1616+
type ReturnType<T extends (...args: never) => any> = T extends (...args: never) => infer R ? R : any;
16171617

16181618
/**
16191619
* Obtain the return type of a constructor function type

src/testRunner/unittests/tsc/incremental.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -609,8 +609,8 @@ console.log(a);`,
609609
fs,
610610
"/lib/lib.d.ts",
611611
Utils.dedent`
612-
type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => infer R ? R : any;
613-
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;`,
612+
type ReturnType<T extends (...args: never) => any> = T extends (...args: never) => infer R ? R : any;
613+
type InstanceType<T extends abstract new (...args: never) => any> = T extends abstract new (...args: never) => infer R ? R : any;`,
614614
),
615615
edits: [
616616
{
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
constraintWithIndexedAccess.ts(23,90): error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: any) => any'.
2-
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
3-
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
4-
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
5-
constraintWithIndexedAccess.ts(24,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
6-
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
7-
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
8-
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
9-
constraintWithIndexedAccess.ts(26,103): error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: any) => any'.
10-
Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
11-
Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: any) => any'.
12-
Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: any) => any'.
13-
constraintWithIndexedAccess.ts(27,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
14-
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
15-
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
16-
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
17-
constraintWithIndexedAccess.ts(28,102): error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'.
18-
Type 'DataFetchFns[T]["Boat"] | DataFetchFns[T]["Plane"]' is not assignable to type '(...args: any) => any'.
19-
Type 'DataFetchFns[T]["Boat"]' is not assignable to type '(...args: any) => any'.
1+
constraintWithIndexedAccess.ts(23,90): error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: never) => any'.
2+
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
3+
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: never) => any'.
4+
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: never) => any'.
5+
constraintWithIndexedAccess.ts(24,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: never) => any'.
6+
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
7+
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: never) => any'.
8+
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: never) => any'.
9+
constraintWithIndexedAccess.ts(26,103): error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: never) => any'.
10+
Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
11+
Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: never) => any'.
12+
Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: never) => any'.
13+
constraintWithIndexedAccess.ts(27,102): error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: never) => any'.
14+
Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
15+
Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: never) => any'.
16+
Type 'DataFetchFns[T][string]' is not assignable to type '(...args: never) => any'.
17+
constraintWithIndexedAccess.ts(28,102): error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: never) => any'.
18+
Type 'DataFetchFns[T]["Boat"] | DataFetchFns[T]["Plane"]' is not assignable to type '(...args: never) => any'.
19+
Type 'DataFetchFns[T]["Boat"]' is not assignable to type '(...args: never) => any'.
2020
constraintWithIndexedAccess.ts(28,102): error TS2536: Type 'T' cannot be used to index type 'DataFetchFns[T]'.
2121
constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to index type 'DataFetchFns'.
22-
constraintWithIndexedAccess.ts(29,102): error TS2344: Type 'DataFetchFns[F][F]' does not satisfy the constraint '(...args: any) => any'.
23-
Type 'DataFetchFns[F][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
24-
Type 'DataFetchFns[F][string] | DataFetchFns[F][number] | DataFetchFns[F][symbol]' is not assignable to type '(...args: any) => any'.
25-
Type 'DataFetchFns[F][string]' is not assignable to type '(...args: any) => any'.
26-
Type 'DataFetchFns[keyof DataFetchFns[T]][string]' is not assignable to type '(...args: any) => any'.
22+
constraintWithIndexedAccess.ts(29,102): error TS2344: Type 'DataFetchFns[F][F]' does not satisfy the constraint '(...args: never) => any'.
23+
Type 'DataFetchFns[F][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
24+
Type 'DataFetchFns[F][string] | DataFetchFns[F][number] | DataFetchFns[F][symbol]' is not assignable to type '(...args: never) => any'.
25+
Type 'DataFetchFns[F][string]' is not assignable to type '(...args: never) => any'.
26+
Type 'DataFetchFns[keyof DataFetchFns[T]][string]' is not assignable to type '(...args: never) => any'.
2727
constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to index type 'DataFetchFns[F]'.
2828

2929

@@ -52,45 +52,45 @@ constraintWithIndexedAccess.ts(29,102): error TS2536: Type 'F' cannot be used to
5252
export type SucceedingCombo = ReturnType<TypeHardcodedAsParameterWithoutReturnType<'Boat', keyof DataFetchFns['Boat']>>;
5353
export type FailingCombo<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<TypeHardcodedAsParameterWithoutReturnType<T,F>>;
5454
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55-
!!! error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: any) => any'.
56-
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
57-
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
58-
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
55+
!!! error TS2344: Type 'TypeHardcodedAsParameterWithoutReturnType<T, F>' does not satisfy the constraint '(...args: never) => any'.
56+
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
57+
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: never) => any'.
58+
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: never) => any'.
5959
export type TypeHardcodedAsParameter<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
6060
~~~~~~~~~~~~~~~~~~
61-
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
62-
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
63-
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
64-
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
61+
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: never) => any'.
62+
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
63+
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: never) => any'.
64+
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: never) => any'.
6565
type VehicleSelector<T extends keyof DataFetchFns> = DataFetchFns[T];
6666
export type TypeHardcodedAsParameter2<T extends 'Boat', F extends keyof DataFetchFns[T]> = ReturnType<VehicleSelector<T>[F]>;
6767
~~~~~~~~~~~~~~~~~~~~~
68-
!!! error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: any) => any'.
69-
!!! error TS2344: Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
70-
!!! error TS2344: Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: any) => any'.
71-
!!! error TS2344: Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: any) => any'.
68+
!!! error TS2344: Type 'VehicleSelector<T>[F]' does not satisfy the constraint '(...args: never) => any'.
69+
!!! error TS2344: Type 'VehicleSelector<T>[keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
70+
!!! error TS2344: Type 'VehicleSelector<T>[string] | VehicleSelector<T>[number] | VehicleSelector<T>[symbol]' is not assignable to type '(...args: never) => any'.
71+
!!! error TS2344: Type 'VehicleSelector<T>[string]' is not assignable to type '(...args: never) => any'.
7272
export type TypeGeneric1<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][F]>;
7373
~~~~~~~~~~~~~~~~~~
74-
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: any) => any'.
75-
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
76-
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: any) => any'.
77-
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: any) => any'.
74+
!!! error TS2344: Type 'DataFetchFns[T][F]' does not satisfy the constraint '(...args: never) => any'.
75+
!!! error TS2344: Type 'DataFetchFns[T][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
76+
!!! error TS2344: Type 'DataFetchFns[T][string] | DataFetchFns[T][number] | DataFetchFns[T][symbol]' is not assignable to type '(...args: never) => any'.
77+
!!! error TS2344: Type 'DataFetchFns[T][string]' is not assignable to type '(...args: never) => any'.
7878
export type TypeGeneric2<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[T][T]>; // error
7979
~~~~~~~~~~~~~~~~~~
80-
!!! error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: any) => any'.
81-
!!! error TS2344: Type 'DataFetchFns[T]["Boat"] | DataFetchFns[T]["Plane"]' is not assignable to type '(...args: any) => any'.
82-
!!! error TS2344: Type 'DataFetchFns[T]["Boat"]' is not assignable to type '(...args: any) => any'.
80+
!!! error TS2344: Type 'DataFetchFns[T][T]' does not satisfy the constraint '(...args: never) => any'.
81+
!!! error TS2344: Type 'DataFetchFns[T]["Boat"] | DataFetchFns[T]["Plane"]' is not assignable to type '(...args: never) => any'.
82+
!!! error TS2344: Type 'DataFetchFns[T]["Boat"]' is not assignable to type '(...args: never) => any'.
8383
~~~~~~~~~~~~~~~~~~
8484
!!! error TS2536: Type 'T' cannot be used to index type 'DataFetchFns[T]'.
8585
export type TypeGeneric3<T extends keyof DataFetchFns, F extends keyof DataFetchFns[T]> = ReturnType<DataFetchFns[F][F]>; // error
8686
~~~~~~~~~~~~~~~
8787
!!! error TS2536: Type 'F' cannot be used to index type 'DataFetchFns'.
8888
~~~~~~~~~~~~~~~~~~
89-
!!! error TS2344: Type 'DataFetchFns[F][F]' does not satisfy the constraint '(...args: any) => any'.
90-
!!! error TS2344: Type 'DataFetchFns[F][keyof DataFetchFns[T]]' is not assignable to type '(...args: any) => any'.
91-
!!! error TS2344: Type 'DataFetchFns[F][string] | DataFetchFns[F][number] | DataFetchFns[F][symbol]' is not assignable to type '(...args: any) => any'.
92-
!!! error TS2344: Type 'DataFetchFns[F][string]' is not assignable to type '(...args: any) => any'.
93-
!!! error TS2344: Type 'DataFetchFns[keyof DataFetchFns[T]][string]' is not assignable to type '(...args: any) => any'.
89+
!!! error TS2344: Type 'DataFetchFns[F][F]' does not satisfy the constraint '(...args: never) => any'.
90+
!!! error TS2344: Type 'DataFetchFns[F][keyof DataFetchFns[T]]' is not assignable to type '(...args: never) => any'.
91+
!!! error TS2344: Type 'DataFetchFns[F][string] | DataFetchFns[F][number] | DataFetchFns[F][symbol]' is not assignable to type '(...args: never) => any'.
92+
!!! error TS2344: Type 'DataFetchFns[F][string]' is not assignable to type '(...args: never) => any'.
93+
!!! error TS2344: Type 'DataFetchFns[keyof DataFetchFns[T]][string]' is not assignable to type '(...args: never) => any'.
9494
~~~~~~~~~~~~~~~~~~
9595
!!! error TS2536: Type 'F' cannot be used to index type 'DataFetchFns[F]'.
9696

tests/baselines/reference/genericRestParameters1.errors.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
genericRestParameters1.ts(135,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
2-
Type 'Function' provides no match for the signature '(...args: any): any'.
1+
genericRestParameters1.ts(135,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: never) => any'.
2+
Type 'Function' provides no match for the signature '(...args: never): any'.
33
genericRestParameters1.ts(164,1): error TS2322: Type '(a: never) => void' is not assignable to type '(...args: any[]) => void'.
44
Types of parameters 'a' and 'args' are incompatible.
55
Type 'any' is not assignable to type 'never'.
@@ -142,8 +142,8 @@ genericRestParameters1.ts(164,1): error TS2322: Type '(a: never) => void' is not
142142
type T08<T extends any[]> = ConstructorParameters<new (...args: T) => void>;
143143
type T09 = Parameters<Function>;
144144
~~~~~~~~
145-
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
146-
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: any): any'.
145+
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: never) => any'.
146+
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: never): any'.
147147

148148
type Record1 = {
149149
move: [number, 'left' | 'right'];

tests/baselines/reference/inferTypes1.errors.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
inferTypes1.ts(36,23): error TS2344: Type 'string' does not satisfy the constraint '(...args: any) => any'.
2-
inferTypes1.ts(37,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'.
1+
inferTypes1.ts(36,23): error TS2344: Type 'string' does not satisfy the constraint '(...args: never) => any'.
2+
inferTypes1.ts(37,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: never) => any'.
3+
Type 'Function' provides no match for the signature '(...args: never): any'.
44
inferTypes1.ts(43,25): error TS2344: Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
55
inferTypes1.ts(44,25): error TS2344: Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
66
Type 'Function' provides no match for the signature 'new (...args: any): any'.
@@ -59,11 +59,11 @@ inferTypes1.ts(153,40): error TS2322: Type 'T' is not assignable to type 'string
5959
type T16 = ReturnType<never>; // never
6060
type T17 = ReturnType<string>; // Error
6161
~~~~~~
62-
!!! error TS2344: Type 'string' does not satisfy the constraint '(...args: any) => any'.
62+
!!! error TS2344: Type 'string' does not satisfy the constraint '(...args: never) => any'.
6363
type T18 = ReturnType<Function>; // Error
6464
~~~~~~~~
65-
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
66-
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: any): any'.
65+
!!! error TS2344: Type 'Function' does not satisfy the constraint '(...args: never) => any'.
66+
!!! error TS2344: Type 'Function' provides no match for the signature '(...args: never): any'.
6767
type T19<T extends any[]> = ReturnType<(x: string, ...args: T) => T[]>; // T[]
6868

6969
type U10 = InstanceType<typeof C>; // C
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/types/conditional/inferTypesBuiltinHelpers.ts] ////
2+
3+
=== inferTypesBuiltinHelpers.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/55667
5+
6+
type _1 = ReturnType<(...args: never) => 'foo'>
7+
>_1 : Symbol(_1, Decl(inferTypesBuiltinHelpers.ts, 0, 0))
8+
>ReturnType : Symbol(ReturnType, Decl(lib.es5.d.ts, --, --))
9+
>args : Symbol(args, Decl(inferTypesBuiltinHelpers.ts, 2, 22))
10+
11+
type _2 = InstanceType<new (...args: never) => 'foo'>
12+
>_2 : Symbol(_2, Decl(inferTypesBuiltinHelpers.ts, 2, 47))
13+
>InstanceType : Symbol(InstanceType, Decl(lib.es5.d.ts, --, --))
14+
>args : Symbol(args, Decl(inferTypesBuiltinHelpers.ts, 3, 28))
15+

0 commit comments

Comments
 (0)