Accessing object values and array members is MUCH more powerful in the type world than it is in the runtime world.
Passing a union... RETURNS a union!
interface ColorVariants {
primary: "blue";
secondary: "red";
tertiary: "green";
}
type PrimaryColor = ColorVariants["primary"]; // "blue"
type NonPrimaryColor = ColorVariants["secondary" | "tertiary"]; // "red" | "green"
type EveryColor = ColorVariants[keyof ColorVariants]; // "blue" | "red" | "green"
type Letters = ["a", "b", "c"];
type AOrB = Letters[0 | 1]; // "a" | "b"
type Letter = Letters[number]; // "a" | "b" | "c"
interface UserRoleConig {
user: ["view", "create", "update"];
superAdmin: ["view", "create", "update", "delete"];
}
type Role = UserRoleConig[keyof UserRoleConig][number] // "view" | "create" | "update" | "delete"
标签:blue,Typescript,deeper,create,objects,green,ColorVariants,type,red From: https://www.cnblogs.com/Answer1215/p/16804106.html