-
Notifications
You must be signed in to change notification settings - Fork 12.8k
extends dynamic Base class with generic throw an error #8853
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
Comments
For the first example, on master, I get a different error: interface I<T> {
new(): T
}
function gen<T>(base: I<T>) {
return class extends base {
^^^^ Base constructor return type 'T' is not a class or interface type
name: string;
}
} This error seems correct to me -- T is just a type variable, so we can't know if it's a class or not. However, adding a constraint doesn't make the error go away: class Foo { }
function gen<T extends Foo>(base: I<T>) {
return class extends base {
^^^^ same error here
name: string
}
} |
this is behaving as i would expect. you can only extend object types, in other words something that of a known structure. type arguments are not. the extend relationship involves checking that the structure of the derived class matches that of the base. without knowing what is the shape of the base, the compiler can not report these errors. The request to allow classes to extend generic type arguments is tracked by #4890 |
Thanks, ahejlsberg example is working interface Base{}
interface BaseClass<T> {
new (): T
}
function generateUserClass<T extends Base>(baseClass: BaseClass<T>){
class User extends (baseClass as BaseClass<Base>) {
name: string;
}
return User as BaseClass<T & User>;
}
class Model{
id: number;
}
var User = generateUserClass(Model);
var user = new User();
user.id; |
'new User()' is ok, but you cannot create a class that extends User. |
Throw error:
Base constructor return type 'T' is not a class or interface type
Error:
Type any is not constructor function type
But if we specify generic it's work
Throw error:
Base constructor return type 'IUser & Base' is not a class or interface type
TS version: 1.8
The text was updated successfully, but these errors were encountered: