gpt4 book ai didi

typescript - "-?"语法在 TypeScript 映射类型中意味着什么?

转载 作者:搜寻专家 更新时间:2023-10-30 20:55:55 25 4
gpt4 key购买 nike

谁能解释一下那个“-?”是什么意思?在以下 TypeScript 类型声明中意味着,与仅使用“?”相比在那里?

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

最佳答案

这不是通配符。 -? TypeScript 2.8 中添加了符号作为 removing the optional modifier 的一种方式来自映射类型。

基本上,这意味着 ValidationMap<T>T 具有相同的键, 但没有 ValidationMap<T> 的属性是可选的,即使 T 的相应属性也是如此是。例如:

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

在这里,Example有一个必需的 b属性,即使它映射的类型有一个可选的 b属性(property)。

(请注意,如果您将 -? 更改为仅 ?+?,它会变成相反的......您将添加可选修饰符。? 符号作为 mapped types 功能的一部分添加到 TypeScript 2.1 中,而 +? 符号是与 -? 一起在 TypeScript 2.8 中引入的。)

希望对您有所帮助。祝你好运!

关于typescript - "-?"语法在 TypeScript 映射类型中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51753015/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com