a normal usage:
I only got string[]
function getResult<T>(...v: T[]) {
return v
}
const str = getResult('str', 'str2') // const str: string[]
// or even
const str = getResult<string>('str' as const, 'str2') // const str: string[]
but if I let the generics extend string, end up with:
// for stirng
function getResultByExtendsString<T extends string>(...v: T[]) {
return v
}
const str2 = getResultByExtendsString('str', 'str2') // const str2: ("str" | "str2")[]
// for number
function getResultByExtendsNumber<T extends number>(...v: T[]) {
return v
}
const num2 = getResultByExtendsNumber(123, 456) // const num2: (123 | 456)[]
or even the mixed types
but if I remove the extending of P, Q, it won't get value but its type
type Types = string | number
function getAllResults<P extends Types, Q extends Types>(p: P, v: Q): (P | Q)[] {
return [p, v]
}
const res = getAllResults('str', 123) // const res: ("str" | 123)[]
so how does this happen?
thanks. see from ts background