我想根据模板类型将泛型类属性初始化为默认值。这在Typescript中可能吗?例如:
class Foo<T> { public x: T; constructor() { this.x = ???; } }
我想设置 ??? (new Foo<number>()).x === 0 和 (new Foo<boolean>).x === false 和 (new Foo<Bar>()).x === null . 有可能吗?
???
(new Foo<number>()).x === 0
(new Foo<boolean>).x === false
(new Foo<Bar>()).x === null
TypeScript类型参数仅在编译时存在。
无法基于类型参数更改值。