Skip to content

Use 'abstract new' in InstanceType and ConstructorParameters #43380

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

Merged
merged 1 commit into from
Mar 25, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1508,7 +1508,7 @@ type Parameters<T extends (...args: any) => any> = T extends (...args: infer P)
/**
* Obtain the parameters of a constructor function type in a tuple
*/
type ConstructorParameters<T extends new (...args: any) => any> = T extends new (...args: infer P) => any ? P : never;
type ConstructorParameters<T extends abstract new (...args: any) => any> = T extends abstract new (...args: infer P) => any ? P : never;

/**
* Obtain the return type of a function type
Expand All @@ -1518,7 +1518,7 @@ type ReturnType<T extends (...args: any) => any> = T extends (...args: any) => i
/**
* Obtain the return type of a constructor function type
*/
type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;

/**
* Convert string literal type to uppercase
Expand Down
20 changes: 5 additions & 15 deletions tests/baselines/reference/inferTypes1.errors.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
tests/cases/conformance/types/conditional/inferTypes1.ts(36,23): error TS2344: Type 'string' does not satisfy the constraint '(...args: any) => any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(37,23): error TS2344: Type 'Function' does not satisfy the constraint '(...args: any) => any'.
Type 'Function' provides no match for the signature '(...args: any): any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(43,25): error TS2344: Type 'string' does not satisfy the constraint 'new (...args: any) => any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(44,25): error TS2344: Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(43,25): error TS2344: Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(44,25): error TS2344: Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
Type 'Function' provides no match for the signature 'new (...args: any): any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(45,25): error TS2344: Type 'typeof Abstract' does not satisfy the constraint 'new (...args: any) => any'.
Cannot assign an abstract constructor type to a non-abstract constructor type.
tests/cases/conformance/types/conditional/inferTypes1.ts(47,42): error TS2344: Type 'abstract new (x: string, ...args: T) => T[]' does not satisfy the constraint 'new (...args: any) => any'.
Cannot assign an abstract constructor type to a non-abstract constructor type.
tests/cases/conformance/types/conditional/inferTypes1.ts(55,25): error TS2344: Type '(x: string, y: string) => number' does not satisfy the constraint '(x: any) => any'.
tests/cases/conformance/types/conditional/inferTypes1.ts(56,25): error TS2344: Type 'Function' does not satisfy the constraint '(x: any) => any'.
Type 'Function' provides no match for the signature '(x: any): any'.
Expand All @@ -25,7 +21,7 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(153,40): error TS2322:
Type 'T' is not assignable to type 'symbol'.


==== tests/cases/conformance/types/conditional/inferTypes1.ts (18 errors) ====
==== tests/cases/conformance/types/conditional/inferTypes1.ts (16 errors) ====
type Unpacked<T> =
T extends (infer U)[] ? U :
T extends (...args: any[]) => infer U ? U :
Expand Down Expand Up @@ -75,20 +71,14 @@ tests/cases/conformance/types/conditional/inferTypes1.ts(153,40): error TS2322:
type U12 = InstanceType<never>; // never
type U13 = InstanceType<string>; // Error
~~~~~~
!!! error TS2344: Type 'string' does not satisfy the constraint 'new (...args: any) => any'.
!!! error TS2344: Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.
type U14 = InstanceType<Function>; // Error
~~~~~~~~
!!! error TS2344: Type 'Function' does not satisfy the constraint 'new (...args: any) => any'.
!!! error TS2344: Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
!!! error TS2344: Type 'Function' provides no match for the signature 'new (...args: any): any'.
type U15 = InstanceType<typeof Abstract>; // Abstract
~~~~~~~~~~~~~~~
!!! error TS2344: Type 'typeof Abstract' does not satisfy the constraint 'new (...args: any) => any'.
!!! error TS2344: Cannot assign an abstract constructor type to a non-abstract constructor type.
type U16<T extends any[]> = InstanceType<new (x: string, ...args: T) => T[]>; // T[]
type U17<T extends any[]> = InstanceType<abstract new (x: string, ...args: T) => T[]>; // T[]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
!!! error TS2344: Type 'abstract new (x: string, ...args: T) => T[]' does not satisfy the constraint 'new (...args: any) => any'.
!!! error TS2344: Cannot assign an abstract constructor type to a non-abstract constructor type.

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

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/inferTypes1.types
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type U14 = InstanceType<Function>; // Error
>U14 : any

type U15 = InstanceType<typeof Abstract>; // Abstract
>U15 : any
>U15 : Abstract
>Abstract : typeof Abstract

type U16<T extends any[]> = InstanceType<new (x: string, ...args: T) => T[]>; // T[]
Expand All @@ -126,7 +126,7 @@ type U16<T extends any[]> = InstanceType<new (x: string, ...args: T) => T[]>; /
>args : T

type U17<T extends any[]> = InstanceType<abstract new (x: string, ...args: T) => T[]>; // T[]
>U17 : any
>U17 : T[]
>x : string
>args : T

Expand Down