TypeScript 中的破折号问号语法是什么?
What is the dash-question mark syntax in TypeScript?
我 不是 意思是 ,相反,我问的是 -?
,例如:
type Required<T> =
T extends object
? { [P in keyof T]-?: NonNullable<T[P]>; } // <---------- "-?" here
: T;
通过 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
?如果不是,这个语法叫什么?我在哪里可以阅读更多相关信息?
你基本上是对的:它从 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) 中删除了可选的 属性 修饰符 ?
,没有它就不可能存在。
令人遗憾的是,TypeScript 文档分散在手册、发行说明、常见问题解答、过时的规范和 GitHub 问题之间,没有明确的规范位置来查找任何特定内容。该语言的发展速度快于文档所能跟上的速度。
好的,希望对你有帮助;祝你好运!
我 不是 意思是 -?
,例如:
type Required<T> =
T extends object
? { [P in keyof T]-?: NonNullable<T[P]>; } // <---------- "-?" here
: T;
通过 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
?如果不是,这个语法叫什么?我在哪里可以阅读更多相关信息?
你基本上是对的:它从 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) 中删除了可选的 属性 修饰符 ?
,没有它就不可能存在。
令人遗憾的是,TypeScript 文档分散在手册、发行说明、常见问题解答、过时的规范和 GitHub 问题之间,没有明确的规范位置来查找任何特定内容。该语言的发展速度快于文档所能跟上的速度。
好的,希望对你有帮助;祝你好运!