In an attempt to create a versatile method that can handle a Mapped Type named QueryParamObject
and partially read its properties:
QueryParamObject
can handle any type and returns a type where all properties are either string[] or string:
export type QueryParamObject<TInput> = {
[key in keyof TInput]: TInput[key] extends Array<string | number> ? string[] : string;
};
Everything functions perfectly when using specific types:
type Concrete = {
numberProp: number;
numberArray: number[];
stringArray: string[];
}
function read(arg: QueryParamObject<Concrete>): void {
const arr1: string[] = arg.numberArray;
const prop1: string = arg.numberProp;
// const arr2: number[] = arg.numberArray; //will have type error
}
However, everything breaks down and each property's resulting type becomes string[] | string
when adding a Generic:
function read<TExtra>(arg: QueryParamObject<TExtra & Concrete>): void {
const arr1: string[] = arg.numberArray; //Type 'string | string[]' is not assignable to type 'string[]'
}
https://i.sstatic.net/j7Eev.png
What is the issue with this notation? How can I get TypeScript to correctly infer those properties even with the use of Generics?