代码之家  ›  专栏  ›  技术社区  ›  Brian Adams

什么是-?你的意思是打字?

  •  9
  • Brian Adams  · 技术社区  · 7 年前

    我在街上碰到这条线 type definitions for prop-types

    export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
    

    没有 - mapped type 但我在文件里找不到它说的任何地方 -?

    有人能解释一下吗 -? 什么意思?

    1 回复  |  直到 7 年前
        1
  •  53
  •   basarat    7 年前

    + - 允许控制映射类型修饰符( ? readonly ). -? 手段必须是全部存在的,也就是说它移除了 可选性 ( ? )例如:

    type T = {
        a: string
        b?: string
    }
    
    
    // Note b is optional
    const sameAsT: { [K in keyof T]: string } = {
        a: 'asdf', // a is required
    }
    
    // Note a became optional
    const canBeNotPresent: { [K in keyof T]?: string } = {
    }
    
    // Note b became required
    const mustBePreset: { [K in keyof T]-?: string } = {
        a: 'asdf', 
        b: 'asdf'  // b became required 
    }
    
    推荐文章