Initially, when you use keyof
, it gives you a union of all keys within a type.
type BadVector = { x: string, y: boolean }
keyof BadVector // "x" | "y"
Hence, the statement K extends keyof any
implies that K
represents such a union type of keys. For instance, if you have the type keyof BadVector
, it would extend keyof any
.
Secondly, in a mapped type, every key in an object gets mapped to a new value type. Therefore, [P in K]: T
signifies: Assign each property P
in K
to a value type T
.
Consider the scenario provided in the documentation example:
interface CatInfo {
age: number;
breed: string;
}
type CatName = "miffy" | "boris" | "mordred";
const cats: Record<CatName, CatInfo> = {
miffy: { age: 10, breed: "Persian" },
boris: { age: 5, breed: "Maine Coon" },
mordred: { age: 16, breed: "British Shorthair" },
};
The datatype of cats
is Record<CatName, CatInfo>
. This indicates that for all types present in CatName
, a corresponding value with the type CatInfo
must be included.