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 use a default for the type parameter, if you want
T2
to be the same asT1
you can useT1
as the default:I would like to raise a different question though. Why have
T2
at all id you don't want to specify it and just default it toT1
. Why not just useT1
. Type parameters that only appear in one position (either return type or a parameter type) are usually suspect. The whole point of type parameters is to establish relations between parameters or between parameters and return type.Without knowing your full use case, this function makes more sense to me: