什么 - ?在TypeScript中意味着什么?

Bri*_*ams 14 typescript

我在类型定义中prop-types遇到了这一行:

export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
Run Code Online (Sandbox Code Playgroud)

没有-它将是一个非常标准的部分映射类型,但我找不到它所谈论的文档中的任何地方-?.

谁能解释一下是什么-?意思?

bas*_*rat 22

+-允许控制映射的类型修饰符(?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 
}
Run Code Online (Sandbox Code Playgroud)

  • 我有点奇怪是你回答的(我正在读你的书)。:) 你的书或 TypeScript 文档中是否涵盖了这一点? (2认同)
  • 引入此语法时也许值得注意:https://github.com/microsoft/TypeScript/commit/a629acd8fd4844742fdd01ab6cf55afa9377db0e 以及用于符号的名称:`MappedTypeModifiers`。 (2认同)
  • 啊?`+?` 有什么作用?它是否相当于“?” - 编辑:答案是肯定的(https://www.typescriptlang.org/docs/handbook/release-notes/overview.html#improved-control-over-mapped-type-modifiers ) (2认同)