I have a generic class, which gets some interface as type parameter called DataType
. I know that all fields of passed interface are optional (but don't know how to inform ts about this) so I want to initialize private field data
with empty object, because in the beginning it is always empty but can be filled with some values
class Basic<DataType extends {}> {
private data: DataType = {} // this line throws ts2322 error
}
class Man extends Basic<{ firstName?: string, age?: number }> {}
class Animal extends Basic<{ name?: string, isPet?:boolean }> {}
but get an error ts2322
Type '{}' is not assignable to type 'DataType'. '{}' is assignable to the constraint of type 'DataType', but 'DataType' could be instantiated with a different subtype of constraint '{}'
Can this problem be solved?