Consider the TypeScript snippet below:
type ForwardVal<T> = {
[K in keyof T]: string;
};
type ForwardKeyOf<T extends string | number | symbol> = {
[K in T]: string;
};
type ByObj = ForwardVal<number[]>; // string[]
type ByKeyOf = ForwardKeyOf<keyof number[]>; // { length: string, toString: string, ... }
type foo = ByObj['push']; // (...items: string[]) => number
type bar = ByKeyOf['push']; // string
What causes foo
to be a function and not a string, while bar
is indeed a string? What distinguishes forwarding a keyof obj
from using Key in T
when forwarding obj
itself and employing Key in keyof T
within a mapped type?
Doesn't the T
parameter simply get swapped out for its assigned value?