I need to create a custom type that is a partial of a generic type, but also allows an array as a property of the corresponding type. For example, if we have the following interface:
interface DataModel {
type: string;
count: number;
}
The resolved type should look like this:
{
type?: string | string[];
count?: number | number[];
}
and behave as follows:
{ type: 'foo'} // valid
{ type: ['foo', 'bar'] } // valid
{ type: 1 } // invalid
I initially created this utility type, but it also allows non-corresponding types for the property:
type KeyOf<T> = Extract<keyof T, string>;
type FilterProps<
T extends object,
U extends KeyOf<T> = KeyOf<T>
> = Partial<Record<U, T[U] | T[U][]>>;
I have provided a playground which illustrates this further
Ideally, I would like the custom type to also reject properties on the model that are not primitive values, although this is not a strict requirement and I may be able to solve it myself.