Given the following code:
declare function test<A = number, B = string>(a: A, b: B): { a: A, b: B }
type T1 = ReturnType<typeof test> // { a: unknown; b: unknown }
type T2 = ReturnType<typeof test<number>> // { a: number; b: string }
Why can’t TS infer T1 correctly? Is it a bug? Are there any workarounds besides T2?
>Solution :
Typescript infers the type unknown correctly, since the types of A and B are not defined as long as the function is not called with arguments. You must define the types with extends like this:
declare function test<A extends number = number, B extends string = string>(a: A, b: B): { a: A, b: B }