question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug] Mixin parameters failed to use as Method Parameter types of Inner Classes

See original GitHub issue

🔎 Search Terms

mixin, parameter, class method, type declaration, decorator

🕗 Version & Regression Information

  • This is a crash
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about mixin & parameter

⏯ Playground Link

Playground link with relevant code

💻 Code

const register: MethodDecorator = () => { }

const validate: ParameterDecorator = () => { }

function mixin<T>(Model: new (...data: any[]) => T) {
    class Trait {
        @register
        method(@validate input: Model) { }
    }
    return Trait;
}

class TestModel {
    id = 0
}

class TestController extends mixin(TestModel) { }

🙁 Actual behavior

Type checking crashed

error TS2749: 'Model' refers to a value, but is being used as a type here. Did you mean 'typeof Model'?

         method(@validate input: Model) {
                                 ~~~~~

Compiled seems to work

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:returntype", void 0)
    ], Trait.prototype, "method", null);
    return Trait;
}

🙂 Expected behavior

Shown sample code works, so that projects with Decorator frameworks will be much simpler, like what my service scaffold does: idea2app/REST-Node-ts#1

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
whzx5bybcommented, Sep 15, 2022

@TechQuery

why should I use typeof in mixins?

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 https://github.com/microsoft/TypeScript/issues/50191 and https://github.com/microsoft/TypeScript/issues/50161. I have reported it in https://github.com/microsoft/TypeScript/issues/50795.

0reactions
TechQuerycommented, 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…

Read more comments on GitHub >

github_iconTop Results From Across the Web

tutorial:mixin_examples [Fabric Wiki]
Mixing into a private inner class. Use the targets parameter and a $ sign to get the inner class. @Mixin(targets = ...
Read more >
python - How do I correctly add type-hints to Mixin classes?
The code works. But running mypy on it, yields the following errors: example.py:4: error: "MultiplicatorMixin" ...
Read more >
How to Use Mixins in Sass and Pass Arguments – With Code ...
"Mixins allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes ......
Read more >
Mixins, Pt.1; Generics, Pt. 1
In the past two weeks, we reviewed classes and interfaces as well as ... by further functionality– which we can do using default...
Read more >
The mixin pattern in TypeScript - all you need to know - Bryntum
This error demonstrates how TypeScript prevents us from using arbitrary properties / methods on a variable with the MyMixinType type.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found