打字稿类型断言运算符优先级

Typescript Type Assertion operator precendence

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

ios ? TouchableOpacity : View as React.ElementType

或者,一如既往 'comes first' 它将使用 '?:' 结果?

或更好的实施将:

(ios ? TouchableOpacity : View) as React.ElementType

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