Say I a flow-based typing with a type that is like:
type baseTy = { x: number, y: number, z: number}
now I wish (for utility) to have a type that is the same as baseTy, but one property different:
type derivTy = {x: string, y: number, z: number}
Obviously I can redeclare it, however this is redundant and might lead to problems when updating later. So I'd like to just define "the difference". How can this be done with flow types?
I could do:
type derivTy = $Diff<baseTy, {x: any}> & {x: string}
However this looks quite verbose and doesn't show the meaning clearly. Is there a better way to do this?
You can spread the type, that's what we do in our codebase to create types that are related:
type BaseT = { a: string, b: number }
type SubBaseT = { ...$Exact<BaseT>, b: string }
const foo: SubBaseT = { a: 'hello', b: 'world' }
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