Skip to content

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

@Hotell

Description

@Hotell
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 ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions