Answer the question
In order to leave comments, you need to log in
Is it possible, and how if so, to write a dynamic interface for React props to have a single key?
There are 2 different interfaces.
interface Name {
name: string;
}
interface LastName {
lastName: number;
}
<Component
name="Ivan"
lastName="" // error lastName is not presented on type Name
/>
<Component
name="Ivan" // error name is not presented on type LastName
lastName="Fenia"
family={['Ivan', 'Anya']}
/>
Answer the question
In order to leave comments, you need to log in
interface Name {
name: string;
}
interface LastName {
lastName: string;
family: string[];
}
function Component(props: Name): React.ReactElement;
function Component(props: LastName): React.ReactElement;
function Component(props: Name | LastName): React.ReactElement {
if ('family' in props) {
return <b>{props.lastName}</b>
} else {
return <b>{props.name}</b>
}
}
What is the problem with combining them?
interface Name {
name?: string;
lastName?: number;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question