【问题标题】:Typescript Type Assertion operator precendenceTypescript 类型断言运算符优先级
【发布时间】:2021-10-05 02:32:36
【问题描述】:

我应该在'as'类型断言上包装js-三元运算符吗?

ios ? TouchableOpacity : View as React.ElementType

或者,因为它总是“首先出现”,所以它会使用 '?:' 结果?

或更好的实施将:

(ios ? TouchableOpacity : View) as React.ElementType

【问题讨论】:

    标签: javascript typescript logical-operators operator-precedence


    【解决方案1】:

    a ? b : c as T 等价于a ? b : (c as T)

    Here's a little demo of the difference:

    Math.random() > .5 ? '' : 0 as string
    //                        ~~~~~~~~~~~
    // Conversion of type 'number' to type 'string' may be a mistake...
    
    // Ok
    (Math.random() > .5 ? '' : 0) as string
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 2011-06-21
      • 2013-02-24
      • 2012-08-10
      相关资源
      最近更新 更多