简体   繁体   English

什么 - ? TypeScript 是什么意思?

[英]What does -? mean in TypeScript?

I came across this line in the type definitions for prop-types :我在prop-types的类型定义中遇到了这一行:

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

Without - it would be a pretty standard partial mapped type , but I can't find anywhere in the docs where it talks about -?没有-这将是一个非常标准的部分映射类型,但我无法在文档中找到它谈论的任何地方-? . .

Can anyone explain what -?任何人都可以解释什么-? means?方法?

+ or - allows control over the mapped type modifier ( ? or readonly ). +-允许控制映射的类型修饰符( ?readonly )。 -? means must be all present, aka it removes optionality ( ? ) eg: 意味着必须全部存在,也就是说它删除了选择性? ),例如:

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 
}

Read -?-? as remove optionality , which makes it required.作为remove optionality ,这使得它成为必需的。 Here is the current official documentation. 是当前的官方文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM