【发布时间】:2016-12-08 17:11:09
【问题描述】:
我正在尝试为我的类使用通用接口。 我的类有一个泛型类型,它扩展了一个接口和一个具有该类型的类变量。但是,一旦我尝试为该变量赋值,编译器就会给我一个错误。(示例:A 类)
当我不扩展通用类型时,它可以工作。 (例:B类)
//Generic Classes problem
interface MyStateInterface {
test?: number
}
class A<IState extends MyStateInterface> {
protected state: IState;
constructor() {
// Error here
this.state = {
test: 1
};
}
}
class B<IState extends MyStateInterface> {
protected state: MyStateInterface;
constructor() {
this.state = {
test: 1
};
}
}
有没有人能解决这个问题?
【问题讨论】:
标签: generics typescript compiler-errors