[英]Typescript Type Assertion operator precendence
Should i wrap js-ternary operator on 'as' Type Assertion?我应该在'as'类型断言上包装js-三元运算符吗?
ios ? TouchableOpacity : View as React.ElementType
or, as it always 'comes first' it will use '?:' result?或者,因为它总是“先来”,它将使用 '?:' 结果?
or better implementation will:或更好的实施将:
(ios ? TouchableOpacity : View) as React.ElementType
a ? b : c as T
a ? b : c as T
is equivalent to a ? b : (c as T)
a ? b : c as T
等价于a ? b : (c as T)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.