any和any []有什么区别? [英] What is the difference between any and any[ ]?

查看:166
本文介绍了any和any []有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

name1: any;
name2: any[];
this.name1 = this.name2;


示例2(这也按预期运行)

name1: any;
name2: any[];
this.name2 = this.name1;


为什么允许any数据类型的打字稿可以访问any[]数据类型.像any[]数据类型一样可以访问any类型的数据吗?和哪个是最适合使用的?如果数据类型只是像string or number or object or any这样的对象(而不是数组),那么为什么any[ ]将不显示该对象而接受该对象类型任何运行时或编译时错误?.

Why typescript allowing any data type can be access any[] data type. As same as any[] data type can be access any type of data? and Which one is best to use? And If the data type is just an object (not an array) like string or number or object or any, then why any[ ] will be accept that object type without showing any run-time or compile-time error?.


推荐答案

如果我们看一下此TypeScript代码:

If we look at this TypeScript code:

let name1: any = "John";
let name2: any[] = ["Mary", "Sue", "Sally"];
name1 = name2;

let name3: any[] = ["Luke", "Paul", "Ringo"];
let name4: any = "Mark";
name3 = name4;

它编译的Javascript是:

The Javascript that it compiles to is:

var name1 = "John";
var name2 = ["Mary", "Sue", "Sally"];
name1 = name2;
var name3 = ["Luke", "Paul", "Ringo"];
var name4 = "Mark";
name3 = name4;

JavaScript变量是动态的,没有设置类型.我可以想象到 可以被TypeScript编译器报告为警告或错误,但这不会停止JavaScript的生成或阻止JavaScript的运行.

JavaScript variables are dynamic and don't have a set type. I can imagine that this could be reported as a warning or error by the TypeScript compiler, but that does not stop the generation of the JavaScript or prevent the JavaScript from running.

因此,尽管目前没有针对any/any[]分配的编译错误或警告,但any[]仍可用于告知开发人员期望.

So while there is no current compile errors or warnings for any/any[] assignments, any[] can still be used to inform the developer of expectations.

注意:这不是我不会编写或建议使用的代码.这只是带有值的OPs代码,并显示最终的JavaScript是什么,并且因为它可以编译为JavaScript,所以不会因为类型而产生运行时错误,因为变量具有动态类型.这是我们选择使用TypeScript的原因之一:编译时静态类型检查.由于这个原因,通常会避免使用anyany[],而选择一种更好地表示数据的类或接口.在这种情况下,stringstring[](或任何其他类型) 将会显示编译器错误.

Note: This is not code I would ever write or suggest that any would ever use. This is just the OPs code with values and showing what the resulting JavaScript would be and that because it compiles to JavaScript, there can be no run-time errors due to type because variables have dynamic type. This is one of the reasons we choose to use TypeScript: compile time static type checking. For this reason, one would normally avoid any and any[] in favor of a class or interface that better represents the data. And in this case, string vs. string[] (or any other type) will show a compiler error.

在此示例中,name1 = name2name3 = name4都是编译时错误,因为变量从分配中推断出类型,并且在后续代码中对它们的用法进行了类型检查.

In this example, both of the name1 = name2 and name3 = name4 are compile time errors as the variables infer the type from the assignment and their usage is type-checked in subsequent code.

let name1 = "John";
let name2 = ["Mary", "Sue", "Sally"];
name1 = name2; //Type 'string[]' is not assignable to type 'string'.

let name3 = ["Luke", "Paul", "Ringo"];
let name4 = "Mark";
name3 = name4; //Type 'string' is not assignable to type 'string[]'.

这篇关于any和any []有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆