Reputation: 13332
I would like initialize a generic class property to a default value based on the template type. Is this possible in Typescript? For example:
class Foo<T> {
public x: T;
constructor() {
this.x = ???;
}
}
I would like to set ???
such that (new Foo<number>()).x === 0
and (new Foo<boolean>).x === false
and (new Foo<Bar>()).x === null
. Is that possible?
Upvotes: 0
Views: 89
Reputation: 887453
TypeScript type parameters only exist at compile-time.
It is impossible to change a value based on a type parameter.
Upvotes: 2