typescript 打字稿数组与任何[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15860715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Typescript Array vs any[]
提问by TheFlash
In TypeScript, what is the difference between Array
and any[]
? Does Array refer to dynamically sized arrays (during compile-time, of course) and any[]
refer to statically sized arrays passed as arguments and type-inferred by the compiler? Because currently when I have functions such as:
在 TypeScript 中,Array
和之间有什么区别any[]
?Array 是否指的是动态大小的数组(当然是在编译时)并any[]
指的是作为参数传递并由编译器类型推断的静态大小的数组?因为目前当我有以下功能时:
mergeArrays(arr1 : Array, arr2 : Array);
When you call this function with static data, TypeScript uses a typed array (type[]
).
当您使用静态数据调用此函数时,TypeScript 使用类型化数组 ( type[]
)。
mergeArrays([true, false], [1, 2]);
// bool[] number[]
So are there any compile-time differences between the 2? When do I use any[]
and when do I use Array
? Sometimes defining vars with Array
..
那么两者之间是否存在编译时差异?any[]
我Array
什么时候用,什么时候用?有时用Array
..
var info:Array;
..show errors like "Cannot convert any[][] to Array" when setting as follows:
.. 设置如下时显示错误,如“无法将任何[][]转换为数组”:
info = [[], []];
采纳答案by Ryan Cavanaugh
Spec section 3.5.4 specifies their relationship:
规范第 3.5.4 节规定了它们的关系:
An array type of the form
ElementType[]
is equivalent to an object type with the index signature[index: number]: ElementType
plus a set of members equivalent to the global interface typeArray
where all occurrences of the magic_element
type are replaced withElementType
.
该表单的数组类型
ElementType[]
等效于具有索引签名的对象类型[index: number]: ElementType
加上一组等效于全局接口类型的成员,Array
其中所有出现的魔法_element
类型都被替换为ElementType
.
Not being able to assign [[], []]
or []
to Array
is a bug.
不能够转让[[], []]
或[]
以Array
是一个错误。
There is no notion of a "statically-sized" array in TypeScript. They're all talking about the same underlying JavaScript array (which is dynamically sized):
TypeScript 中没有“静态大小”数组的概念。他们都在谈论相同的底层 JavaScript 数组(动态调整大小):
var x = [1, 2];
x.push(3);
x[13] = 5;
回答by zoran404
The previous answer to this question has been outdated for a while now.
之前对这个问题的回答已经过时了一段时间。
First of all Array
requires a generic parameter:
首先Array
需要一个泛型参数:
var arr: Array<any>;
This is equivalent to:
这相当于:
var arr: any[];
Types Array<any>
and any[]
are identical and both refer to arrays with variable/dynamic size.
类型Array<any>
和any[]
是相同的,并且都是指具有可变/动态大小的数组。
Typescript 3.0 introduced Tuples, which are like arrays with fixed/static size, but not really.
Typescript 3.0 引入了元组,类似于具有固定/静态大小的数组,但实际上并非如此。
Let me explain.
让我解释。
Their syntax looks like this:
它们的语法如下所示:
var arr: [any];
var arr: [any, any];
var arr: [any, any, any];
var arr: [string, number, string?, MyType];
Typescript will force you to assign an array of that length and with values that have the matching types.
And when you index the array typescript will recognize the type of variable at that index.
Typescript 将强制您分配一个具有该长度的数组,并使用具有匹配类型的值。
当您索引数组打字稿时,它将识别该索引处的变量类型。
It's interesting to note that typescript wont stop you from modifying the size of the Tuple, either by using a function like .push()
or by assigning a value to length
. So you have to make sure you don't do it by accident.
有趣的是,打字稿不会阻止您修改元组的大小,无论是使用类似的函数.push()
还是为length
. 所以你必须确保你不会无意中这样做。
You can also specify a "rest" type for any extra elements:
您还可以为任何额外元素指定“休息”类型:
var arr: [any, any, ...any[]];
var arr: [string, number, string?, MyType, ...YourType[]];
In which case you can assign more items to the Tuple and the size of the array may change.
在这种情况下,您可以将更多项分配给元组,数组的大小可能会发生变化。
Note: You can not assign a normal array to a Tuple.
注意:您不能将普通数组分配给元组。
In case you only specify a rest type, then you get a regular old array.
The following 2 expressions are equivalent:
如果您只指定休息类型,那么您将获得一个常规的旧数组。
以下 2 个表达式是等效的:
var arr: [...any[]];
var arr: any[];