打字稿是否支持?.操作员?(还有,它叫什么?) [英] Does Typescript support the ?. operator? (And, what's it called?)

查看:20
本文介绍了打字稿是否支持?.操作员?(还有,它叫什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Typescript 目前(或有计划)是否支持 safe? 的导航操作符.

Does Typescript currently (or are there plans to) support the safe navigation operator of ?.

即:

var thing = foo?.bar
// same as:
var thing = (foo) ? foo.bar : null;

另外,这个运营商有没有更通用的名字(谷歌搜索非常困难).

Also, is there a more common name for this operator (it's incedibly hard to google for).

推荐答案

更新:从 TypeScript 3.7 开始支持它并称为可选链:https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining

Update: it is supported as of TypeScript 3.7 and called Optional chaining: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining

我在 TypeScript 语言中找不到任何对它的引用规范.

至于在 CoffeeScript 中如何调用这个运算符,它被称为 存在运算符(具体来说,是存在运算符的访问器变体").

As far as what to call this operator in CoffeeScript, it's called the existential operator (specifically, the "accessor variant" of the existential operator).

来自 CoffeeScript 关于运算符的文档:

存在运算符 ?. 的访问器变体可用于在属性链中吸收空引用.在基值可能为 nullundefined 的情况下,使用它代替点访问器 ..

The accessor variant of the existential operator ?. can be used to soak up null references in a chain of properties. Use it instead of the dot accessor . in cases where the base value may be null or undefined.

因此,存在运算符的访问器变体似乎是引用该运算符的正确方式;并且 TypeScript 目前似乎不支持它(尽管 其他人表达了对这个功能的渴望).

So, the accessor variant of the existential operator appears to be the proper way to refer to this operator; and TypeScript does not currently appear to support it (although others have expressed a desire for this functionality).

这篇关于打字稿是否支持?.操作员?(还有,它叫什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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