typescript 打字稿中的可选链运算符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45843668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Optional Chaining Operator in Typescript
提问by cwtuan
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. Any idea?
但我在 Typescript 中找不到如何做到这一点。任何的想法?
回答by Ryan Cavanaugh
At time of writing, TypeScript does not support the optional chaining operator. See discussion on the TypeScript issue tracker: https://github.com/Microsoft/TypeScript/issues/16
在撰写本文时,TypeScript 不支持可选链运算符。请参阅有关 TypeScript 问题跟踪器的讨论:https: //github.com/Microsoft/TypeScript/issues/16
As a warning, the semantics of this operator are still verymuch 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. I generally recommend people to not start using syntax whose behavior hasn't been well-defined yet.
作为一个警告,这个操作符的语义仍然非常多的流量,这就是为什么打字稿还没有添加它。今天针对 Babel 插件编写的代码可能会在没有警告的情况下在未来改变行为,从而导致严重的错误。我通常建议人们不要开始使用行为尚未明确定义的语法。
回答by protoEvangelion
Update Oct 15, 2019
2019 年 10 月 15 日更新
Support now exists in [email protected]
支持现在存在于 [email protected]
Say thanks to https://stackoverflow.com/a/58221278/6502003for 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 articlechronicles 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 Cavanaughfor all the work you are doing in advocating this operator to TC39!
此外,非常感谢Ryan Cavanaugh在向 TC39 宣传此运营商方面所做的所有工作!
回答by José Salgado
Typescript 3.7 beta has now support for Optional chaining
You can now write code like this:
您现在可以编写如下代码:
let x = foo?.bar?.baz;