TypeScript转换数组 [英] TypeScript casting arrays
本文介绍了TypeScript转换数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图为希望使用数组作为输入参数的库使用包装器。
I'm trying to use a wrapper for a library that wants an Array as an input parameter.
我尝试转换数组,但出现错误:无法将'any []'转换为'Array'
I tried casting the Array, but I get an error: Cannot convert 'any[]' to 'Array'
有没有办法做到这一点?
Is there a way to make this work?
var rows = new Array(10);
var rows2 = <Array>rows; //<--- Cannot convert 'any[]' to 'Array'
推荐答案
TypeScript中有4种可能的数组转换方法:
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;
as
运算符主要用于 *。tsx
文件以避免语法歧义。
The as
operator's mostly designed for *.tsx
files to avoid the syntax ambiguity.
这篇关于TypeScript转换数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文