P粉2677913262023-09-06 20:12:26
In TypeScript, when you define a type using the intersection & of two or more types, the resulting type will have all the properties of each intersection type. In this case, PersonSpan is defined as the intersection of Person and Lifespan, so an object of type PersonSpan must have all the properties of Person and Lifespan. Even if keyof PersonSpan results in "name" | "birth" | "death", this does not mean that any object with only one of these properties is valid for PersonSpan, it means that the type K you initialized is the union of the PersonSpan property names. Set, you can access these properties using the key "name" on an object of type PersonSpan, "birth" or "death" which may also be the Partial type you are looking for, which makes all props optional
let obj: Partial<PersonSpan> = { name: "John Smith" }