-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
class constraints #9776
Comments
How would TS check that the anonymous class is compatible with Eg class Foo {
public fooMethod(x:number):string { return 'hello'; }
}
export function create<T>(base: { new(): T }) {
return new class extends base {
public fooMethod(x:string):void {}
};
}
create<Foo>(Foo); The anonymous class is overriding the |
A good question, it is certainly possible:
|
Seems that @ahejlsberg already shown some workaround for the problem described by @Aleksey-Bykov, could not find, though. @Aleksey-Bykov, I completely agree that we need something like that. However @jesseschalken is also right. I'd propose other solution, that can look way to far from your solution (class constraint) but will help to solve the same problem. I would propose the special type constructor: var a: {x: number} overrides {x: {}, y: boolean};
// and get the correct { x: number, y: boolean} As well as var X: TExtension overrides TBase;
// where both TExtension and TBase are generic type params This operator ensures that extension is correct at the instantiation time. var a: {x: number} overrides {x: string, y: boolean};
// error: number and string are not compatible Guys, what do you think? |
in my case, @jesseschalken example would look like: export function create<T>(base: new() => T }): {
fooMethod(x: string): void
} overrides T {
return new class extends base {
public fooMethod(x:string):void {}
};
}
class Foo {
public fooMethod(x:number):string { return 'hello'; }
}
create<Foo>(Foo); // error at compile time |
For all actual types ping @RyanCavanaugh |
I like |
looks like a duplicate of #4890 |
@mhegazy - exactly, it's that duplicate I've tried to find. @Aleksey-Bykov let's go there and move the process forward -) |
I am looking for a way to get my derived class be inherited from a generic class.
This is my naive attempt to get it:
It might be that I am just doing it wrong, but looks like a current limitation, if so:
It might require a new syntax for class constraints:
The text was updated successfully, but these errors were encountered: