-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript
Description
TypeScript Version: 2.4.0
It would be interesting to allow type hinting for class type.
Something like
function generateMyClass(myClass: class) {
return new myClass();
}
class Foo {}
generateMyClass(Foo); // Should work
generateMyClass('something else'); // should NOT workCurrently, the closest we have is
type Instantiable = {new(...args: any[]): any};
function doSomethingWithMyClass(myClass: Instantiable ) {
// some code that doesn't matter
}
class Foo {}
abstract class Bar {}
doSomethingWithMyClass(Foo); // It works :)
doSomethingWithMyClass('something else' ); // It doesn't work and it's OK
doSomethingWithMyClass(Bar); // It doesn't work and it's NOT OKIt is of course logical that the abstract class causes an error with the code above, this is why it could be nice to have something to handle this case.
To go further, we can think about adding some genericity on it.
// With the actual implementation
type Instantiable<T = any> = {new(...args: any[]): T};
function foo<T>(myClass: Instantiable<T>): T { /* ... */ }
// With the new class type hint
function foo<T>(myClass: class<T>): T { /* ... */ }SamPruden, Supamiu, FabianLauer, ajafff, cvx and 46 more
Metadata
Metadata
Assignees
Labels
Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScriptAn idea for TypeScript