Typescript: Decorators within mixins

Created on 12 Mar 2017  ·  8Comments  ·  Source: microsoft/TypeScript

I wonder if there's any plans to support decorators within mixins?

  type Constructor<T> = { new (...args: any[]): T }

  function Mixin<T extends Constructor<{}>>(Base : T) {
    return class extends Base {
      constructor(...args: any[]) {
        super(...args)
      }

      @decorator
      get y() {
        return this.x + 3
      }
  }

TSC 2.2.1 currently rejects this construction: [Decorators are not valid here]

Duplicate

Most helpful comment

We got into same issues here https://github.com/wc-catalogue/blaze-elements/blob/master/packages/common/mixins.ts#L10

the quickfix is easy just define the mixin class and return it afterwards:

  type Constructor<T> = { new (...args: any[]): T }

  function Mixin<T extends Constructor<{}>>(Base : T) {
    class MixinClass extends Base {
      constructor(...args: any[]) {
        super(...args)
      }

      @decorator
      get y() {
        return this.x + 3
      }
    }

    return MixinClass;
}

All 8 comments

We got into same issues here https://github.com/wc-catalogue/blaze-elements/blob/master/packages/common/mixins.ts#L10

the quickfix is easy just define the mixin class and return it afterwards:

  type Constructor<T> = { new (...args: any[]): T }

  function Mixin<T extends Constructor<{}>>(Base : T) {
    class MixinClass extends Base {
      constructor(...args: any[]) {
        super(...args)
      }

      @decorator
      get y() {
        return this.x + 3
      }
    }

    return MixinClass;
}

Ah, Wonderful!

Thanks!

Hmm I would rather keep it open to get core team opinion on this cc @RyanCavanaugh @DanielRosenwasser - if it's desing limitation or bug.
What I suggested is just "hotfix" to not get stuck

thx!

True.

I think the idea that decorators can inform the type system of changes that it may be performing is something we will be considering over time.

Cool so I guess we can close this and reference the hotfix to #7342 ?

Sorry, I misread this. My previous comment has nothing to do with this, but yeah, looks like #7432 seems like the right place to discuss the issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

blendsdk picture blendsdk  ·  3Comments

Antony-Jones picture Antony-Jones  ·  3Comments

MartynasZilinskas picture MartynasZilinskas  ·  3Comments

bgrieder picture bgrieder  ·  3Comments

fwanicka picture fwanicka  ·  3Comments