Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flow "override" properties in object-type to new type

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?

like image 789
paul23 Avatar asked Oct 23 '25 20:10

paul23


1 Answers

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' }
like image 105
Andrew Smith Avatar answered Oct 26 '25 12:10

Andrew Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!