TypeScript:创建一个空的类型化容器数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15826745/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:02:36  来源:igfitidea点击:

TypeScript: Creating an empty typed container array

javascriptarraystypescript

提问by darethas

I am creating simple logic game called "Three of a Crime" in TypeScript.

我正在用 TypeScript 创建名为“犯罪三项”的简单逻辑游戏。

When trying to pre-allocated typed array in TypeScript, I tried to do something like this:

在 TypeScript 中尝试预分配类型化数组时,我尝试执行以下操作:

var arr = Criminal[];

which gave the error "Check format of expression term" .

这给出了错误“检查表达式术语的格式”。

also tried doing this

也试过这样做

var arr : Criminal = [];

and this produced "cannot convert any[] to 'Criminal'

这产生了“无法将任何[]转换为‘犯罪’

what is the 'TypeScript' way to do this?

什么是“打字稿”的方式来做到这一点?

采纳答案by Thorarin

The existing answers missed an option, so here's a complete list:

现有的答案错过了一个选项,所以这里有一个完整的列表:

// 1. Explicitly declare the type
var arr: Criminal[] = [];

// 2. Via type assertion
var arr = <Criminal[]>[];
var arr = [] as Criminal[];

// 3. Using the Array constructor
var arr = new Array<Criminal>();
  1. Explicitly specifying the type is the general solution for whenever type inference fails for a variable declaration.

  2. The advantage of using a type assertion(sometimes called a cast, but it's not really a cast in TypeScript) works for any expression, so it can be used even when no variable is declared. There are two syntaxes for type assertions, but only the latter will work in combination with JSX if you care about that.

  3. Using the Array constructor is something that will only help you in this specific use case, but which I personally find the most readable. However, there is a slight performance impactat runtime*. Also, if someone were crazy enough to redefine the Array constructor, the meaning could change.

  1. 显式指定类型是当变量声明的类型推断失败时的通用解决方案。

  2. 使用类型断言(有时称为强制转换,但它在 TypeScript 中并不是真正的强制转换)的优点适用于任何表达式,因此即使没有声明变量也可以使用它。类型断言有两种语法,但如果您关心的话,只有后者才能与 JSX 结合使用。

  3. 使用 Array 构造函数只会在这个特定用例中为您提供帮助,但我个人认为这是最易读的。但是,在运行时会对性能产生轻微影响*。此外,如果有人疯狂到重新定义 Array 构造函数,其含义可能会改变

It's a matter of personal preference, but I find the third option the most readable. In the vast majority of cases the mentioned downsides would be negligible and readability is the most important factor.

这是个人喜好的问题,但我发现第三个选项最具可读性。在绝大多数情况下,上述缺点可以忽略不计,可读性是最重要的因素。

*: Fun fact; at the time of writing the performance difference was 60% in Chrome, while in Firefox there was no measurable performance difference.

*: 有趣的事实; 在撰写本文时,Chrome 中的性能差异为 60%,而 Firefox 中没有可测量的性能差异。

回答by darethas

The issue of correctly pre-allocating a typed array in TypeScript was somewhat obscured for due to the array literal syntax, so it wasn't as intuitive as I first thought.

由于数组文字语法,在 TypeScript 中正确预分配类型化数组的问题有些模糊,所以它并不像我最初想象的那样直观。

The correct way would be

正确的方法是

var arr : Criminal[] = [];

This will give you a correctly typed, empty array stored in the variable 'arr'

这将为您提供一个正确键入的空数组,该数组存储在变量“arr”中

Hope this helps others!

希望这对其他人有帮助!

回答by Fylax

I know this is an old question but I recently faced a similar issue which couldn't be solved by this way, as I had to return an empty array of a specific type.

我知道这是一个老问题,但我最近遇到了一个无法通过这种方式解决的类似问题,因为我必须返回一个特定类型的空数组。

I had

我有

return [];

where []was Criminal[]type.

这里[]Criminal[]类型。

Neither return: Criminal[] [];nor return []: Criminal[];worked for me.

既不对我来说有效return: Criminal[] [];也不return []: Criminal[];对我来说有效。

At first glance I solved it by creating a typed variable (as youcorrectly reported) just before returning it, but (I don't know how JavaScript engines work) it may create overhead and it's less readable.

乍一看,我通过在返回之前创建一个类型化变量(如正确报告的那样)来解决它,但是(我不知道 JavaScript 引擎如何工作)它可能会产生开销并且可读性较差。

For thoroughness I'll report this solution in my answer too:

为了彻底,我也会在我的回答中报告这个解决方案:

let temp: Criminal[] = [];
return temp;

Eventually I found TypeScript type casting, which allowed me to solve the problem in a more concise and readable (and maybe efficient) way:

最终我找到了 TypeScript 类型转换,它使我能够以更简洁和可读(也许更高效)的方式解决问题:

return <Criminal[]>[];

Hope this will help future readers!

希望这对未来的读者有所帮助!

回答by rk13

For publicly access use like below:

对于公开访问使用如下:

public arr: Criminal[] = [];