bugzpodder
bugzpodder

Reputation: 29

how to extend object types for flow

I want to be able to extend props A for a component for a wrapper component by adding a few more fields (props C). When I use a spread operator flow gives an error.

type A = {a: string}
type C = {b: number} & A //{b: number, a:string} 
const c : C = {a: 'a', b: 1}
const {b, ...a} = c;
const a2 : A = a;

This gives an flow error 6: const a2 : A = a; ^ rest of object pattern.

Whats the way around this?

https://flow.org/try/#0C4TwDgpgBAglC8UDeBDAXFAzsATgSwDsBzAXwChRIoBhBZAIwwIFcBbeiHEqAMligD0ApIygt2nADRR02fMW5kyAYwD2BbFGVQMtRKgwByFIemiAjOTUbgDaQDpHKbomUBuFes0oATDv6IKG5AA

Upvotes: 1

Views: 901

Answers (2)

MichaelDeBoey
MichaelDeBoey

Reputation: 2385

flow has a lot of problems with spread operators, like you can see in the issues.
Best workaround at the moment is to explicitly extract each attribute, like Vivek Doshi said.

Upvotes: 0

Vivek Doshi
Vivek Doshi

Reputation: 58543

Here is the work around to it:

type A = {a: string}
type C = {b: number} & A //{b: number, a:string} 

const c : C = {a: 'a', b: 1}
const {b, a} = c;
const a2 : A = {a};

Link to : FLOW

Upvotes: 2

Related Questions