我试图为一个需要 Array 作为输入参数的库使用一个包装器。
我尝试强制转换 Array,但是得到一个错误: 无法将‘ any []’转换为‘ Array’
有办法解决吗?
var rows = new Array(10); var rows2 = <Array>rows; //<--- Cannot convert 'any[]' to 'Array'
I think this is just a bug - can you log an issue on the CodePlex site?
As a workaround, you can write <Array><any>rows;
<Array><any>rows
I think the right syntax is:
var rows2 = <Array<any>>rows;
That's how you cast to interface Array<T>
interface Array<T>
There are 4 possible conversion methods in TypeScript for arrays:
let x = []; //any[] let y1 = x as number[]; let z1 = x as Array<number>; let y2 = <number[]>x; let z2 = <Array<number>>x;
The as operator's mostly designed for *.tsx files to avoid the syntax ambiguity.
as
*.tsx
A simple solution for all types
const myArray = <MyType[]>value;