You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug] Mixin parameters failed to use as Method Parameter types of Inner ClassesΒ #50792
A class declaration does create value and type entities but a variable only create the value one. In your case Model is just a variable refers to the class, so it doesn't create a type entity.
If I use both type & value of classes, your hack code shows a type error in my VS Code 1.71.0
I believe this is a bug, which is similar to #50191 and #50161. I have reported it in #50795.
Activity
whzx5byb commentedon Sep 15, 2022
The error message is correct.
Modelrefers to a value of a function parameter and you should usetypeofto refer to the type of it.See https://www.typescriptlang.org/docs/handbook/2/typeof-types.html.
TechQuery commentedon Sep 15, 2022
I know that
typeofis the "correct syntax", but if I change it like that, Parameter Decorator will failed to get emited classes:const register: MethodDecorator = () => { } const validate: ParameterDecorator = () => { } function mixin<T>(Model: new (...data: any[]) => T) { class Trait { @register - method(@validate input: Model) { } + method(@validate input: typeof Model) { } } return Trait; } class TestModel { id = 0 } class TestController extends mixin(TestModel) { }function mixin(Model) { var _a; class Trait { method(input) { } } __decorate([ register, __param(0, validate), __metadata("design:type", Function), - __metadata("design:paramtypes", [typeof (_a = typeof Model !== "undefined" && Model) === "function" ? _a : Object]), + __metadata("design:paramtypes", [Object]), __metadata("design:returntype", void 0) ], Trait.prototype, "method", null); return Trait; }whzx5byb commentedon Sep 15, 2022
I think this workaround should work:
function mixin<T>(Model: new (...data: any[]) => T) { + type Model = typeof Model; class Trait { @register method(@validate input: Model) { } } return Trait; }TechQuery commentedon Sep 15, 2022
whzx5byb Thanks your hack code, but I wonder that
classsyntax is both type & value, why should I usetypeofin mixins?TechQuery commentedon Sep 15, 2022
whzx5byb If I use both type & value of classes, your hack code shows a type error in my VS Code 1.71.0:
but
tsccompiling with the sametypescript@4.8.3innode_modulesworks fine...TS playground & GitPod's VS Code 1.69.2 works fine, too...
whzx5byb commentedon Sep 15, 2022
South Drifter (@TechQuery)
A class declaration does create value and type entities but a variable only create the value one. In your case
Modelis just a variable refers to the class, so it doesn't create a type entity.I believe this is a bug, which is similar to #50191 and #50161. I have reported it in #50795.
HerringtonDarkholme commentedon Sep 15, 2022
Minimal reproduction.
compiles to
It looks like the behavior of
typeof Expris not handled properly in decorator metadata.TechQuery commentedon Sep 15, 2022
whzx5byb Thanks your explanation & issue reporting.
When I open my GitPod VS Code again, the same error hints...
Just like #50161 said: sometimes...