【发布时间】:2019-02-24 07:02:56
【问题描述】:
我在type definitions for prop-types看到了这条线:
export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
如果没有-,这将是一个相当标准的部分mapped type,但我在文档中找不到任何地方提到-?。
谁能解释-?是什么意思?
【问题讨论】:
标签: typescript
我在type definitions for prop-types看到了这条线:
export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
如果没有-,这将是一个相当标准的部分mapped type,但我在文档中找不到任何地方提到-?。
谁能解释-?是什么意思?
【问题讨论】:
标签: typescript
+ 或 - 允许控制映射类型修饰符(? 或 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
}
我上过关于这些映射类型修饰符的课程:https://www.youtube.com/watch?v=0zgWo_gnzVI?
【讨论】:
MappedTypeModifiers。