I am kinda a newbie with TypeScript, and I am using it on a real project along with React.
I saw a post where the writter indicates two ways of declare conditional props in React using Typescript.
The writer indicates two ways to declare conditional props in React using TypeScript.
I tried the solution using generics (because I want to approach it that way), and I did not get TypeScript warning me about the type that should be the prop called “level” after checking if the authenticated prop was true.
Here’s the code:
type Props = { authenticated: T; level: T extends true ? ‘basic’ | ‘admin’ : ‘guest’};
const User = (props: Props) => { if (props.authenticated)
{
return <>Profile ({props.level})</>; } return <>{props.level}</>
};