TypeScript 有空条件运算符吗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45441086/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:46:52  来源:igfitidea点击:

Does TypeScript have a Null-conditional operator

typescripttypescript2.0

提问by Da Yin

Is there any operator like ?. in TypeScript that can check if the variable is null or not defined like Kotlin? Like

有没有像这样的运营商?在可以检查变量是否为空或未像 Kotlin 一样定义的 TypeScript 中?喜欢

person?.getName()?.firstName ?: "None"

采纳答案by Loonquawl

No, as of now safe navigation operatior is still not implemented in Typescript: https://github.com/Microsoft/TypeScript/issues/16

不,到目前为止,安全导航操作仍未在 Typescript 中实现:https: //github.com/Microsoft/TypeScript/issues/16

However according to the latest standardization meeting notes it has been proposed, so maybe v3 :)

然而,根据最新的标准化会议记录,它已被提出,所以可能是 v3 :)

https://github.com/tc39/agendas/blob/master/2017/07.md

https://github.com/tc39/agendas/blob/master/2017/07.md

回答by Andrii Stashko

I've encountered the same situation, and the following worked for me.

我遇到了同样的情况,以下对我有用。

First, I defined a separate class with a property that can be null:

首先,我定义了一个单独的类,其属性可以是null

export class Foo {
    id: number; //this can be null
}

Then in my main class I set a property footo this new type:

然后在我的主类中,我将一个属性设置foo为这种新类型:

foo: Foo

After that, I was able to use it like this:

在那之后,我可以像这样使用它:

var fooId = (this.foo || ({} as Foo)).id;

This is the closest I could get to have a null propagation in a current version of TypeScript.

这是我在当前版本的 TypeScript 中最接近 null 传播的方法。

回答by wal

Yes, as of Typescript 3.7 you can now do this via optional-chaining

是的,从 Typescript 3.7 开始,您现在可以通过可选链来执行此操作

person?.getName()?.firstName

gets transpiled to

被转译为

let firstName = person === null || person === void 0 ? void 0 : (_person$getName = person.getName()) === null || _person$getName === void 0 ? void 0 : _person$getName.firstName;

Note the check for null. This will work as expected if for example person is defined as

请注意检查是否为空。如果例如人被定义为,这将按预期工作

let person:any = null; //no runtime TypeError when calling person?.getName()

However if person is defined as

但是,如果人被定义为

let person:any = {};//Uncaught TypeError: person.getName is not a function

See also this similar stackoverflow question

另见这个类似的stackoverflow问题

回答by Ebuka

I used this in my code to check for null

我在我的代码中使用它来检查 null

this.firstname = (this.firstname != null) ? this.firstname : this.firstname;

This could do till v3 is out

这可以做,直到 v3 出来

回答by ApriOri

Actually this is related to javascript null safety discussionthat is mentioned in this answer. I guess they want it to be supported on javascript before they will get to typescript.

实际上,这与此答案中提到的javascript null 安全讨论有关。我猜他们希望在他们开始打字稿之前它在 javascript 上得到支持。