什么是-?在TypeScript中是什么意思?

布莱恩·亚当斯(Brian Adams):

我在以下类型的定义中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 
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章