In TypeScript I know how to declare a multiple key-value pairs object
{ [key: string]: any }
How can I declare a single key-value pair?
The specific use-case I want to support is a prop that can be an array of either string
or a single key-pair object.
For example:
const searchArray = [
'name',
{stats: 'resolution'},
'uptime',
{config: 'interface'},
];
At first I thought the solution was simply
Record<string,string>
So the final declaration for my searchArray
would be
interface Props {
(...)
searchArray: (string | Record<string,string>)[],
}
But I expect it to reject this because I sent two key-pair in an object but it accepts it.
searchArray={[
'name',
'bitrate',
{stats:'resolution', stats:'frameRate'}
]}
It should accept only one key-pair per object. The following should be accepted
searchArray={[
'name',
'bitrate',
{stats:'resolution'},
{stats:'frameRate'}
]}
Thanks!