I've seen this extends syntax:
export type MyType<Data extends Base = Base> = { data: Data }
What does the usage of Base = Base do?
The TypeScript documentation about generics doesn't explain it.
Default generic parameter - docs
It allows you to call MyType without generic parameter.
type Base = { tag: 'Base' }
export type MyType<Data extends Base = Base> = { data: Data }
// type WithArgument = {
// data: {
// tag: 'Base';
// name: 'John';
// };
// }
type WithArgument = MyType<{ tag: 'Base', name: 'John' }>
// { data: Base; }
type Result = MyType
Pure js analogy:
const myType = (arg = { tag: 'Base' }) => arg
const result = myType() // { tag: 'Base' }
This Data extends Base = Base means:
This is how it can be splited into two parts:
Data extends BaseData = BaseYou should not read Base = Base separately from extends word
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With