【发布时间】:2019-01-15 15:47:23
【问题描述】:
我在使用 Typescript 进行依赖注入时遇到了一些问题。在每个类上,我添加了一个工厂静态方法,其中设置了所有依赖项。我这样做是为了测试目的,以便我仍然能够使用 TDD 方法。
现在我在子类中重写父类的工厂方法时遇到了一些问题。示例:
interface DepsA {
a: string
}
interface DepsB extends DepsA {
b: Child1
}
class Parent {
constructor(protected deps: DepsA | DepsB) {}
public static factory<T extends Parent>() {
return new this({a: 'This is a nice Dependency'}) as T
}
}
class Child1 extends Parent {}
class Child2 extends Parent {
public static factory() {
return new this({a: 'This is a nice Dependency', b: Child1.factory()})
}
}
const child1 = Child1.factory<Child1>()
const child2 = Child2.factory()
我收到的错误是:
[ts]
Class static side 'typeof Child2' incorrectly extends base class static side 'typeof Parent'.
Types of property 'factory' are incompatible.
Type '() => Child2' is not assignable to type '<T extends Parent>() => T'.
Type 'Child2' is not assignable to type 'T'.
我知道我为什么会收到错误,但现在不知道如何修复它,除了重命名 Child2 中的工厂静态方法。
更新: 与此问题相关的错误报告,自动解释了为什么我在工厂方法上使用 Generic 是:#26298
【问题讨论】:
标签: node.js typescript oop dependency-injection tdd