zoukankan      html  css  js  c++  java
  • [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

    Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from properties.

    For example, we have an interface:

    interface IPet { 
        name: string;
        age: number;
        favoritePark?: string
    }

    There is two required props and one favoriatePark as optional prop.

    From TypeScirpt 2.8, we are able to gereate a new interface based on existing one, and add or remove props:

    For example we want to remove all the optional props, we can use '-':

    interface IPetRequired {
      [K in keyof IPET]-?: IPet[K]
    }

    '-': remove

    '?': optional

    '-?': remove optional

    We can also use '+' to indicate what we have added:

    type ReadonlyPet = {
        +readonly [K in keyof IPet]?: IPet[K]
    }

    Here we added readonly type.

  • 相关阅读:
    输出国际象棋&&输出余弦曲线
    打鱼晒网问题
    ATM模拟程序
    getline()函数
    AC小笔记
    ural 1208 Legendary Teams Contest
    汇编贪吃蛇
    供给与需求的市场力量
    垄断竞争
    相互依存性和贸易的好处
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10284223.html
Copyright © 2011-2022 走看看