【问题标题】:What is the dash-question mark syntax in TypeScript?TypeScript 中的破折号问号语法是什么?
【发布时间】:2020-08-03 16:46:08
【问题描述】:

我不是指的是question mark syntax,而是我问的是-?,例如:

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

通过this 2018 GitHub comment。我在 TypeScript 手册的 advanced types 和 utility types 章节中找不到这种语法。

上面的

-? 仍然可以在 TypeScript 3.8 中编译,并且似乎是 ? 的 对立面,即是否需要密钥?是否等同于Required?如果不是,这个语法叫什么,我在哪里可以阅读更多关于它的信息?

【问题讨论】:

    标签: typescript syntax optional


    【解决方案1】:

    您基本上是对的:它从mapped type 中删除了可选的属性修饰符?。它是在 TypeScript 2.8 中作为 improvements to control over mapped type modifiers (see this link for the docs you want) 的一部分引入的。它与Required 实用程序类型相同,但Required 是implemented with it(请参阅library definition here),没有它就无法存在。

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

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

    【讨论】:

      猜你喜欢
      • 2019-02-14
      • 2013-05-30
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 2021-01-24
      • 2012-03-25
      • 1970-01-01
      相关资源
      最近更新 更多