My goal is to determine the generic type of Model
for each property. Currently, everything is displaying as unknown[]
instead of the desired types outlined in the comments below.
class Model<T> { x?: T }
type ArgumentType<T> = T extends Model<infer TInner> ? Model<TInner> : (
T extends Model<infer TInner>[] ? Model<TInner>[] : never
);
type ReturnType<T> = T extends Model<infer TInner> ? TInner[] : (
T extends Model<infer TInner>[] ? TInner[][] : never
);
const test = <TInner, TValue extends ArgumentType<TInner>, T extends Record<string, TValue>>(models: T): Record<keyof T, ReturnType<TValue>> => {
return;
}
const numberModel = new Model<number>();
const stringModel = new Model<string>();
const bigintModel = new Model<bigint>();
const result = test({
prop1: [numberModel, numberModel, numberModel],
prop2: [stringModel],
prop3: stringModel,
prop4: [numberModel, numberModel, numberModel, bigintModel],
prop5: stringModel
});
//expected types
result.prop1 // number[][]
result.prop2 // string[][]
result.prop3 // string[]
result.prop4 // (number | bigint)[][]
result.prop5 // string[]
//actual types
result.prop1 // unknown[]
result.prop2 // unknown[]
result.prop3 // unknown[]
result.prop4 // unknown[]
result.prop5 // unknown[]