简体   繁体   中英

Typescript Type Assertion operator precendence

Should i wrap js-ternary operator on 'as' Type Assertion?

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) .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM