class GenericNumber<T> {
zeroValue: T;
add: (x: T, y: T) => T;
}
编译失败:
tsc 3.4.3
src/ts/more.ts:225:3 - error TS2564: Property 'zeroValue' has no initializer and is not definitely assigned in the constructor.
src/ts/more.ts:226:3 - error TS2564: Property 'add' has no initializer and is not definitely assigned in the constructor.
官方文档竟然编译失败,求解.谢谢
这跟是不是泛型无关啊,只要是个成员都是需要初始化的,要么定义时初始化,要么在构建函数里赋值。
比如,你可以
zeroValue?: T
,相当于赋undefined
或者通过构造函数参数传值进来