我应该将 js-ternary 运算符包装在 'as' 类型断言上吗?
ios ? TouchableOpacity : View as React.ElementType
或者,因为它总是“先来”,它会使用“?:”结果?
或更好的实施将:
(ios ? TouchableOpacity : View) as React.ElementType
答案 0 :(得分: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