blogposts icon indicating copy to clipboard operation
blogposts copied to clipboard

Discriminant Unions and React props - why,how,what ?

Open Hotell opened this issue 6 years ago • 0 comments

type Props =
  | { moo: string; children: React.ReactChild }
  | { as: 'button'; foo: number; children: React.ReactChild }

function Button(props: Props) {
  return <React.Fragment>{props.children}</React.Fragment>
}

function App(){
return <>
 <Button as="button" foo={12312}>hello</Button>
 /* NO ERROR */
 <Button moo="what" foo={13333}>world</Button>
</>
}

Let's cover how it can be solved ;)

Hotell avatar Jul 31 '19 08:07 Hotell