TypeScript 中的破折号问号语法是什么? [英] What is the dash-question mark syntax in TypeScript?

查看:104
本文介绍了TypeScript 中的破折号问号语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不是的意思是问号语法,而是我在问关于-?中,例如:

I don't mean the question mark syntax, rather, I'm asking about -? in, for example:

type Required<T> =
  T extends object
    ? { [P in keyof T]-?: NonNullable<T[P]>; } // <---------- "-?" here
    : T;

通过 2018 年 GitHub 评论.我在 TypeScript 手册的 高级类型实用程序类型.

via this 2018 GitHub comment. I cannot find this syntax in the TypeScript handbook's chapters on advanced types nor utility types.

-? 仍然可以在 TypeScript 3.8 中编译,并且看起来与 ?相反,即使密钥成为必需的?它是否等同于 Required?如果没有,这种语法叫什么,我可以在哪里阅读更多相关信息?

-? as above still compiles in TypeScript 3.8, and appears to be the opposite of ?, i.e., making the key required? Is it equivalent to Required? If not, what is this syntax called and where can I read more about it?

推荐答案

你基本上是对的:它从 映射类型.它作为 控制映射类型修饰符的改进(有关您想要的文档,请参阅此链接).它与Required<相同/code> 实用程序类型,但 Required用它实现(见此处为库定义),没有它就无法存在.

You are basically right: it removes the optional property modifier ? from a mapped type. It was introduced in TypeScript 2.8 as part of improvements to control over mapped type modifiers (see this link for the docs you want). It's not identical to the Required utility type, but Required is implemented with it (see library definition here) and could not exist without it.

令人遗憾的是,TypeScript 文档分散在手册、发行说明、常见问题解答、过时的规范和 GitHub 问题之间,没有明确的规范位置来查找任何特定内容.该语言的发展速度比文档还快.

It's a sad fact that TypeScript documentation is kind of spread out between the handbook, release notes, FAQ, outdated spec, and GitHub issues, without a clear canonical place to look for any particular thing. The language has been evolving more quickly than the documentation can keep up.

好的,希望有帮助;祝你好运!

Okay, hope that helps; good luck!

这篇关于TypeScript 中的破折号问号语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆