【发布时间】:2017-09-27 00:43:14
【问题描述】:
众所周知,类型转换在 TypeScript 中称为断言类型。以及以下代码部分:
// the variable will change to true at onetime
let isPlay: boolean = false;
let actions: string[] = ['stop', 'play'];
let action: string = actions[<number> isPlay];
编译时出错
Error:(56, 35) TS2352: Neither type 'boolean' nor type 'number' is assignable to the other.
然后我尝试使用any 类型:
let action: string = actions[<number> <any> isPlay];
也会出错。我该如何重写这些代码。
【问题讨论】:
-
actions[isPlay ? 1 : 0]
标签: javascript typescript typecast-operator