I got a problem when using generic overload functions like below (playground).
The generic type T1 is not used in the parameters (just used in return type), so when I try to use overload #2, I have to provide all the types like f<string, string>('123') which sometimes is not possible. Is there a way to let f<string>('123') match overload #2?
function f<T1, T2>(t2: T2): T1
function f<T1>(): T1
function f<T1, T2>(t2?: T2): T1 | void {
}
f<string, string>('123') // ok
f<string>() // ok
f<string>('123') // error here, can we fix it by not using f<string, string>('123') ?
function f<T1, T2>(t2: T2): T1
function f<T1>(): T1
function f<T1, T2>(t2?: T2): T1 | void {
}
f < string, {id: number}>({id: 123}) // ok
f<string>() // ok
f<string>({id:123}) // typescript complain here can we fix it by not using f<string, string>({id:123}) ?
You can add a fallback for T2 to
unknownorany:Or simply have one declaration for all the cases: