DEV Community

Discussion on: Default Props in React/TypeScript

Collapse
 
qpwo profile image
Luke Harold Miles

For future readers, I think this is the best way. I wrote a short post about it dev.to/qpwo/closest-thing-to-struc...

function Car(props: {
    model: string
    owner: [string, string]
    year?: number
    make?: string
}): JSX.Element {
    props = {
        ...{
            year: 1999,
            make: "toyota",
        },
        ...props
    }
    return <div>
        {props.owner[0]} {props.owner[1]}{"'s"} {props.year} {props.make} {props.model} goes vroom.
    </div>
}
Enter fullscreen mode Exit fullscreen mode