我在prop-types的类型定义中发现了这行代码:
export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
没有
-
,它就是一个相当标准的部分映射类型,但我找不到文档中任何介绍 -?
的地方。有人能解释一下
-?
是什么意思吗?我在prop-types的类型定义中发现了这行代码:
export type ValidationMap<T> = { [K in keyof T]-?: Validator<T[K]> };
-
,它就是一个相当标准的部分映射类型,但我找不到文档中任何介绍 -?
的地方。-?
是什么意思吗?+
或 -
可以控制映射类型修饰符 (?
或 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
。 - Beau+?
是干嘛用的?它和仅仅使用 ?
是等价的吗?——编辑:答案是肯定的(https://www.typescriptlang.org/docs/handbook/release-notes/overview.html#improved-control-over-mapped-type-modifiers) - Simon Farshid