I need a type for my component. There are two fields title and description. They both can be undefined. But description cannot be without title.
Here is correct examples: <Component title='str' description='str'/>
or <Component title='str'/>
or <Component/>
but this one is incorrect <Component description='str'/>
I try something like this
type Props = { title?: string; description?: string } | { title?: never; description?: never };
but that does not work
2
Answers
Something like this should work:
However, since
{}
represents any non-nullish value rather than strictly an "empty object," you should useRecord<string, never>
to explicitly define an empty object type:You can define the type as below:
TS playground: