搜索

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


发布时间: 2022-11-24 17:51:03    浏览次数:43 次

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"

 

免责声明 [!Typescript] Tips: Access deeper parts of objects and arrays,资源类别:文本, 浏览次数:43 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-24 05:51:03。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/Answer1215/p/16804106.html