简体   繁体   English

TypeScript映射类型中的“ - ?”语法是什么意思?

[英]What does the “-?” syntax mean in TypeScript mapped types?

Can anybody please explain what that "-?" 任何人都可以解释一下“ - ?” in the following TypeScript type declarations means, compared to just using a "?" 在下面的TypeScript类型声明意味着,与仅使用“?”相比 there? 那里?

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

It's not a wildcard. 这不是通配符。 The -? -? notation was added in TypeScript 2.8 as a way of removing the optional modifier from mapped types. 在TypeScript 2.8中添加了表示法,作为从映射类型中删除可选修饰符的一种方法。

Basically, it means that ValidationMap<T> has the same keys as T , but none of the properties of ValidationMap<T> is optional, even if the corresponding property of T is. 基本上,这意味着ValidationMap<T>具有与T相同的键,但ValidationMap<T>任何属性都不是可选的,即使T的相应属性是。 For example: 例如:

type Example = ValidationMap<{a: string, b?: number | undefined}>;
// type Example = {a: Validator<string>, b: Validator<number | undefined>}

Here, Example has a required b property even though the type it's mapped from has an optional b property. 这里, Example具有必需的b属性,即使它映射的类型具有可选的b属性。

(Note that if you change -? to just ? or to +? , it becomes the opposite... you would be adding the optional modifier. The ? notation was added in TypeScript 2.1 as part of the mapped types feature, while the +? notation was introduced in TypeScript 2.8 along with -? .) (注意,如果你改变-?只是?或者+?它就会相反......你会添加可选的修饰符。 ?作为映射类型功能的一部分,在TypeScript 2.1中添加了?符号,而+?符号是在TypeScript 2.8中引入的-? 。)

Hope that helps. 希望有所帮助。 Good luck! 祝好运!

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

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