Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

InstanceType<Type> #14

Open
nmsn opened this issue Dec 11, 2022 · 1 comment
Open

InstanceType<Type> #14

nmsn opened this issue Dec 11, 2022 · 1 comment

Comments

@nmsn
Copy link
Contributor

nmsn commented Dec 11, 2022

Constructs a type consisting of the instance type of a constructor function in Type.

class C {
  x = 0;
  y = 0;
}
 
type T0 = InstanceType<typeof C>;
// type T0 = C

type T1 = InstanceType<any>;     
// type T1 = any

type T2 = InstanceType<never>;     
// type T2 = never

type T3 = InstanceType<string>;
// Type 'string' does not satisfy the constraint 'abstract new (...args: any) => any'.  
// type T3 = any

type T4 = InstanceType<Function>;
// Type 'Function' does not satisfy the constraint 'abstract new (...args: any) => any'.
// Type 'Function' provides no match for the signature 'new (...args: any): any'.  
// type T4 = any
@nmsn
Copy link
Contributor Author

nmsn commented Dec 11, 2022

/**
 * Obtain the return type of a constructor function type
 */
type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant