Typescript conversion to boolean

后端 未结 8 568
名媛妹妹
名媛妹妹 2021-02-02 05:19

In Typescript I can do this:

var xxx : some_type;

if (xxx)
    foo();
else
    bar();

Here xxx will be treated as a boolean, regardless of its

8条回答
  •  甜味超标
    2021-02-02 05:34

    You can use this trick which Typescript does allow and which works fine in JavaScript:

    foo(!!xxx);
    

    Alternatively, cast it to any

    foo(xxx);
    

提交回复
热议问题