首页 > 其他分享 >[!Typescript] Tips: Access deeper parts of objects and arrays

[!Typescript] Tips: Access deeper parts of objects and arrays

时间:2022-10-18 21:02:39浏览次数:65  
标签:blue Typescript deeper create objects green ColorVariants type red

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

相关文章