【问题标题】:Typescript mixins doesn't work with generic from parentTypescript mixins 不适用于父级的泛型
【发布时间】:2019-02-13 16:45:51
【问题描述】:

我正在尝试在 typescript 中实现 mixins,并从 mixin 类中获得自动完成功能。这是我的代码:

export type Constructor<T> = new (...args: any[]) => T;

export interface Test {
  methodA();
  methodB();
}


export type TestCtor = Constructor<Test>;


export function mixinTest<T extends Constructor<{}>>(base: T): TestCtor & T {
  return class extends base {

    constructor(...args: any[]) { super(...args); }

    methodA() { }

    methodB() {}

  };
}

class A<T> { 
  value: T;

  constructor(param: string) {}
}

class B extends mixinTest(A<{id: string}>) {
}

new B().methodA();

但我得到了错误:没有基本构造函数有数字 指定的参数

或使用泛型:

预期有 1 个参数,但得到了 3 个

我错过了什么?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    解决方案

    ....
    class _Base extends A<{ id: string }> { }
    
    class B extends mixinTest(_Base) {
    }
    
    new B('Hey!').methodA();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-20
      • 2023-02-11
      • 1970-01-01
      • 2012-08-27
      • 2023-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多