-
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
Can't access to static member of generic class from a decorator. #17454
Comments
You can't access that property because you never declared it in the type bound for |
Well, as this is labeled as question, I'm going to make a question about what I want to achieve. Is it possible to have a generic type T extended from Entity, where I can access static elements from Entity without explicitly have to write twice the Entity's structure or doing any casting? People are suggesting things at https://stackoverflow.com/questions/45347630/accessing-static-member-of-generic-class-from-a-decorator but all suggestions are casting or writing twice the structure. |
Another example of what I think that it shouldn't be mandatory to do: function decorator<T extends typeof Entity>(C: T) {
return class extends (C as typeof Entity) { // Why is this cast necessary?
constructor(...args: {}[]) {
super(...args);
C.member.forEach((v) => {
// ...
});
}
x = 1;
y = 2;
static z = 3;
} as {} as T;
} This code is from StackOverflow example, where a cast from C to |
If your decorator is only going to apply to exactly one class, you could just use |
@lilezek thanks to @andy-ms's explanation, I realized I was mistaken in my answer in SO. To get this to work, That is class Entity {
constructor(...args: any[]) {} // the ... is also required.
} Any type that is used to refine the constraint also must contain the signature function decorator<T extends
typeof Entity
& {
new(...args: any[]): any,
prototype: {p: number}
}
>(C: T) {
return class extends C {};
} will work but the following will fail function decorator<T extends
typeof Entity
& {new(...args: any[]): any}
& {prototype: {p: number}}
>(C: T) {
return class extends C {};
} |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
Stack overflow url of this issue: https://stackoverflow.com/questions/45347630/accessing-static-member-of-generic-class-from-a-decorator
TypeScript Version: nightly (2.5.0-dev.20170725)
Code
Expected behavior:
I would expect to be able to access the static
member
from theDecorator
.Actual behavior:
Member is not part of type T.
The text was updated successfully, but these errors were encountered: