简体   繁体   English

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

[英]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.

相关问题 && JavaScript 中的运算符优先级 - && Operator precendence in JavaScript 通过在 Typescript 中添加类型断言来更改推理 - Change the inference by adding a type assertion in Typescript Typescript - 具有无效值的类型断言不会引发错误 - Typescript - type assertion with invalid value is not throwing error TypeScript 中的这些类型断言或强制转换方法有什么区别 - What are the difference between these type assertion or casting methods in TypeScript TypeScript / JSX类型声明仅在JSX外部有效 - TypeScript/JSX Type Assertion works only outside JSX 在 TypeScript 类型中使用一元加运算符 - Use unary plus operator in a type of TypeScript Typescript / Angular:对于boolean类型,等效于plus运算符 - Typescript / Angular : equivalent of plus operator for boolean type Typescript object const 断言键入 -.includes() “'string' 类型的参数不可分配给 'never' 类型的参数。” - Typescript object const assertion typing - .includes() “Argument of type 'string' is not assignable to parameter of type 'never'.” 在三元运算符内进行类型检查后,打字稿未推断出正确的类型 - Typescript not inferring correct type after type check inside a ternary operator Typescript 将两个 object 与传播运算符合并并具有可重用的类型 - Typescript merging two object with spread operator and having a resuable type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM