[英]Optional Chaining Operator in Typescript
In javascript, Optional Chaining Operator is supported by the babel plugin .在javascript中, babel插件支持Optional Chaining Operator。
But I can't find how to do this in Typescript.但是我在 Typescript 中找不到如何做到这一点。 Any idea?
有什么想法吗?
At time of writing, TypeScript does not support the optional chaining operator.在撰写本文时,TypeScript 不支持可选链运算符。 See discussion on the TypeScript issue tracker: https://github.com/Microsoft/TypeScript/issues/16
请参阅有关 TypeScript 问题跟踪器的讨论: https : //github.com/Microsoft/TypeScript/issues/16
As a warning, the semantics of this operator are still very much in flux, which is why TypeScript hasn't added it yet.作为一个警告,这个操作符的语义仍然非常的流量,这就是为什么打字稿还没有添加它。 Code written today against the Babel plugin may change behavior in the future without warning, leading to difficult bugs.
今天针对 Babel 插件编写的代码将来可能会在没有警告的情况下改变行为,从而导致严重的错误。 I generally recommend people to not start using syntax whose behavior hasn't been well-defined yet.
我通常建议人们不要开始使用行为尚未明确定义的语法。
Support now exists in typescript@3.7.0-beta
现在支持
typescript@3.7.0-beta
Say thanks to https://stackoverflow.com/a/58221278/6502003 for the update!感谢https://stackoverflow.com/a/58221278/6502003的更新!
Although TypeScript and the community are in favor of this operator, until TC39 solidifies the current proposal ( which at the time of this writing is at stage 1
) we will have to use alternatives.尽管 TypeScript 和社区都支持这个操作符,但在 TC39 巩固当前的提案(在撰写本文时处于
stage 1
)之前,我们将不得不使用替代方案。
There is one alternative which gets close to optional chaining without sacrificing dev tooling : https://github.com/rimeto/ts-optchain有一种替代方法可以在不牺牲开发工具的情况下接近可选链: https : //github.com/rimeto/ts-optchain
This article chronicles what the creators were able to achieve in trying to mirror the native chaining operator: 这篇文章记录了创建者在尝试镜像本地链操作符时能够实现的目标:
- Use a syntax that closely mirrors chained property access
使用与链式属性访问密切相关的语法
- Offer a concise expression of a default value when traversal fails
遍历失败时提供默认值的简明表达
- Enable IDE code-completion tools and compile-time path validation
启用 IDE 代码完成工具和编译时路径验证
In practice it looks like this:在实践中它看起来像这样:
import { oc } from 'ts-optchain';
// Each of the following pairs are equivalent in result.
oc(x).a();
x && x.a;
oc(x).b.d('Default');
x && x.b && x.b.d || 'Default';
oc(x).c[100].u.v(1234);
x && x.c && x.c[100] && x.c[100].u && x.c[100].u.v || 1234;
Keep in mind that alternatives like this one will likely be unnecessary once the proposal is adopted by TypeScript.请记住,一旦提案被 TypeScript 采纳,像这样的替代方案可能就没有必要了。
Also, a big thanks to Ryan Cavanaugh for all the work you are doing in advocating this operator to TC39!此外,非常感谢Ryan Cavanaugh在向 TC39 宣传此运营商方面所做的所有工作!
Typescript 3.7 beta has now support for Optional chaining 👍🎉 Typescript 3.7 beta 现在支持可选链👍🎉
You can now write code like this:您现在可以编写如下代码:
let x = foo?.bar?.baz;
Since TypeScript 3.7 was released you can use optional chaining now. 自TypeScript 3.7发布以来,您现在可以使用可选链接。
Property example: 属性示例:
let x = foo?.bar.baz();
This is equvalent to: 这等效于:
let x = (foo === null || foo === undefined) ?
undefined :
foo.bar.baz();
Moreover you can call: 此外,您可以致电:
Optional Call 可选通话
function(otherFn: (par: string) => void) {
otherFn.?("some value");
}
otherFn will be called only if otherFn won't be equal to null or undefined 仅当otherFn不等于null或未定义时,才会调用otherFn
Usage optional chaining in IF statement IF语句中的用法可选链接
This: 这个:
if (someObj && someObj.someProperty) {
// ...
}
can be replaced now with this 现在可以用这个代替
if (someObj?.bar) {
// ...
}
Ref. 参考 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.