Consider this straightforward property access function
export function accessProperty<T, K extends keyof T, P extends T[K]>(name: K, v: T): P {
return v[name] as P
}
What is the significance of the cast as P
in this context?
I experimented with different approaches
export function accessProperty<T, K extends keyof T, P = T[K]>(name: K, v: T): P
export function accessProperty<T, K extends keyof T, P extends T[K] = T[K]>(name: K, v: T): P
yet the cast remains necessary in all cases