I want to create a TypeScript type that involves a set of fixed keys, plus a set of alternate groups of keys, such as:
type Props = {
id: string
} & (
{
height: number,
width: number
}
| {color: string}
)
Now, in this case, it correctly enforces that each of the groups have all the properties of that group, or none of them, i.e.: I can't use height without also using width.
Now, if I want to also allow an alternative without any of the optional keys, the only option that I know actually gives the desired result is by using {} as an alternative.
But lint rule @typescript-eslint/ban-types disallows that, saying that depending on the use case I should either Record<string, unknown>, unknown or Record<string, never>.
But none of these really work for this case. If I use unknown or Record<string, never> or object TS doesn't allow any of the keys in the alternatives. If I use Record<string, unknown> it doesn't allow any key unless I fill one of the alternative key groups.
Is there another way of doing this that I'm missing, or should I ignore the lint rule to achieve this?
Propsabove will also allow{ id: 'i', width: 0, color: 'c' }, and if you add the{}, it will even allow{ id: 'i', height: 0 }: tsplay.dev/mLyQKW