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


    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"
  • 相关阅读:
    Unity3d启动事件
    UI 科学
    LOL
    流光
    PlayerPrefs
    C++
    Android Home
    状态机
    架构设计
    AI
  • 原文地址:https://www.cnblogs.com/Answer1215/p/16804106.html
Copyright © 2020-2023  润新知