What is the difference between any and any[ ]?
Example 1 (working as expected)
name1: any;
name2: any[];
this.name1 = this.name2;
Example 2 (This is also working as expected)
name1: any;
name2: any[];
this.name2 = this.name1;
Why typescript allowing
any
data type can be accessany[]
data type. As same asany[]
data type can be accessany
type of data? and Which one is best to use? And If the data type is just an object (not an array) likestring or number or object or any
, then whyany[ ]
will be accept that object type without showing any run-time or compile-time error?.为什么允许任何数据类型的typescript可以访问任何[]数据类型。与任何[]数据类型一样,可以访问任何类型的数据吗?哪一个最好用?如果数据类型只是一个对象(不是数组),如字符串或数字或对象或任何,那么为什么任何[]将接受该对象类型而不显示任何运行时或编译时错误?
4 个解决方案
#1
10
If we look at this TypeScript code:
如果我们看一下这个TypeScript代码:
let name1: any = "John";
let name2: any[] = ["Mary", "Sue", "Sally"];
name1 = name2;
let name3: any[] = ["Luke", "Paul", "Ringo"];
let name4: any = "Mark";
name3 = name4;
The Javascript that it compiles to is:
它编译的Javascript是:
var name1 = "John";
var name2 = ["Mary", "Sue", "Sally"];
name1 = name2;
var name3 = ["Luke", "Paul", "Ringo"];
var name4 = "Mark";
name3 = name4;
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.
JavaScript变量是动态的,没有集合类型。我可以想象,这可以被TypeScript编译器报告为警告或错误,但这并不会停止生成JavaScript或阻止JavaScript运行。
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.
因此,虽然没有任何/任何[]任务的当前编译错误或警告,但任何[]仍可用于通知开发人员期望。
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.
注意:这不是我写过的代码或建议任何人都会使用的代码。这只是带有值的OPs代码,并显示了生成的JavaScript会是什么,因为它编译为JavaScript,因类型而没有运行时错误,因为变量具有动态类型。这是我们选择使用TypeScript的原因之一:编译时静态类型检查。出于这个原因,通常会避免任何和任何[]支持更好地表示数据的类或接口。在这种情况下,string vs. string [](或任何其他类型)将显示编译器错误。
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.
在此示例中,name1 = name2和name3 = name4都是编译时错误,因为变量从赋值中推断出类型,并且在后续代码中对其用法进行了类型检查。
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[]'.
#2
6
If you use any
, both on the left and on the right side of an assignment, you are basically say to Typescript not use type checking.
如果在赋值的左侧和右侧都使用any,则基本上对Typescript说不使用类型检查。
In any
is on left side it means the the variable you want to assign the value can accept any type, and therefore it accepts anything including an object of type any[]
.
在任何在左侧,它意味着您要分配的变量值可以接受任何类型,因此它接受任何包括任何类型[]的对象的任何东西。
If the type any
is on the right side and you have myVar: any[]
on the left side, you are basically say to Typescript not use type checking. Considering that type checking is not applied at run time, you would end up with myVar
being filled with whatever you may have on the right side of the assignment.
如果类型any在右侧并且你有myVar:左侧的任何[],你基本上是对Typescript说不使用类型检查。考虑到在运行时不应用类型检查,最终会使myVar充满您在赋值右侧的任何内容。
#3
1
- only Any used for single object of any type.
- only任何用于任何类型的单个对象。
- Any[] is for array of objects with type Any.
- 任何[]用于类型为Any的对象数组。
#4
1
Difference between any and any[] is in Intellisence.
任何和任何[]之间的区别在于Intellisence。
var a:any = .....
a.map , a.join (no intellisense, typescript does not know if a is an array or not)
var a:any[] = ....
a.map , a.join (typescript knows that a is an array and will provide intellisence)
Compile time errors
编译时间错误
var aa:any[] = "a"; // error typescript will not allow this
var a:any = "a"; // a can be string, there is no harm
aa = a; // typescript cannot detect error at
// compile time because a can be array
var someString:string;
aa = someString; // typescript knows that aa is an array and someString is not
By declaring something any[]
, you are telling that I want this object to be of type an array. However, any is literally anything, even array can be any
so typescript allows you to assign any to any array.
通过声明任何[],你告诉我希望这个对象是一个数组类型。但是,任何字面上都是任何东西,甚至数组也可以是任何类型,因此typescript允许您将任何数组分配给任何数组。
TypeScript will detect compile time errors whenever it could, but when you set something to any, it will not detect compile time errors.
TypeScript会尽可能地检测编译时错误,但是当你设置任何东西时,它不会检测编译时错误。
It is upto compiler designer to enforce such compile time error or not, I guess following should not be allowed,
编译器设计者是否可以强制执行此类编译时错误,我想以下不应该被允许,
a:any
aa:any[] = a; // this should be compile time error
To avoid, one can certainly cast it as aa = a as any[]
, same as in languages like C# and Java.
为了避免,人们当然可以将其作为aa = a表示为任何[],与C#和Java等语言相同。
Runtime error
运行时错误
JavaScript has no type specification for declaration of variable, so JavaScript engine does not have any idea of about type.
JavaScript没有变量声明的类型规范,因此JavaScript引擎对类型没有任何了解。
Only when you invoke a method or access property, JavaScrip will give you an error..
只有在调用方法或访问属性时,JavaScrip才会给您一个错误。
a:any[] ..
// this will give an error TypeError: .map is not a function
a.map( ()=> ... )
a.length // this is undefined.. any member of any object is
// essentially undefined, there is no type error when you access
// member
#1
10
If we look at this TypeScript code:
如果我们看一下这个TypeScript代码:
let name1: any = "John";
let name2: any[] = ["Mary", "Sue", "Sally"];
name1 = name2;
let name3: any[] = ["Luke", "Paul", "Ringo"];
let name4: any = "Mark";
name3 = name4;
The Javascript that it compiles to is:
它编译的Javascript是:
var name1 = "John";
var name2 = ["Mary", "Sue", "Sally"];
name1 = name2;
var name3 = ["Luke", "Paul", "Ringo"];
var name4 = "Mark";
name3 = name4;
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.
JavaScript变量是动态的,没有集合类型。我可以想象,这可以被TypeScript编译器报告为警告或错误,但这并不会停止生成JavaScript或阻止JavaScript运行。
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.
因此,虽然没有任何/任何[]任务的当前编译错误或警告,但任何[]仍可用于通知开发人员期望。
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.
注意:这不是我写过的代码或建议任何人都会使用的代码。这只是带有值的OPs代码,并显示了生成的JavaScript会是什么,因为它编译为JavaScript,因类型而没有运行时错误,因为变量具有动态类型。这是我们选择使用TypeScript的原因之一:编译时静态类型检查。出于这个原因,通常会避免任何和任何[]支持更好地表示数据的类或接口。在这种情况下,string vs. string [](或任何其他类型)将显示编译器错误。
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.
在此示例中,name1 = name2和name3 = name4都是编译时错误,因为变量从赋值中推断出类型,并且在后续代码中对其用法进行了类型检查。
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[]'.
#2
6
If you use any
, both on the left and on the right side of an assignment, you are basically say to Typescript not use type checking.
如果在赋值的左侧和右侧都使用any,则基本上对Typescript说不使用类型检查。
In any
is on left side it means the the variable you want to assign the value can accept any type, and therefore it accepts anything including an object of type any[]
.
在任何在左侧,它意味着您要分配的变量值可以接受任何类型,因此它接受任何包括任何类型[]的对象的任何东西。
If the type any
is on the right side and you have myVar: any[]
on the left side, you are basically say to Typescript not use type checking. Considering that type checking is not applied at run time, you would end up with myVar
being filled with whatever you may have on the right side of the assignment.
如果类型any在右侧并且你有myVar:左侧的任何[],你基本上是对Typescript说不使用类型检查。考虑到在运行时不应用类型检查,最终会使myVar充满您在赋值右侧的任何内容。
#3
1
- only Any used for single object of any type.
- only任何用于任何类型的单个对象。
- Any[] is for array of objects with type Any.
- 任何[]用于类型为Any的对象数组。
#4
1
Difference between any and any[] is in Intellisence.
任何和任何[]之间的区别在于Intellisence。
var a:any = .....
a.map , a.join (no intellisense, typescript does not know if a is an array or not)
var a:any[] = ....
a.map , a.join (typescript knows that a is an array and will provide intellisence)
Compile time errors
编译时间错误
var aa:any[] = "a"; // error typescript will not allow this
var a:any = "a"; // a can be string, there is no harm
aa = a; // typescript cannot detect error at
// compile time because a can be array
var someString:string;
aa = someString; // typescript knows that aa is an array and someString is not
By declaring something any[]
, you are telling that I want this object to be of type an array. However, any is literally anything, even array can be any
so typescript allows you to assign any to any array.
通过声明任何[],你告诉我希望这个对象是一个数组类型。但是,任何字面上都是任何东西,甚至数组也可以是任何类型,因此typescript允许您将任何数组分配给任何数组。
TypeScript will detect compile time errors whenever it could, but when you set something to any, it will not detect compile time errors.
TypeScript会尽可能地检测编译时错误,但是当你设置任何东西时,它不会检测编译时错误。
It is upto compiler designer to enforce such compile time error or not, I guess following should not be allowed,
编译器设计者是否可以强制执行此类编译时错误,我想以下不应该被允许,
a:any
aa:any[] = a; // this should be compile time error
To avoid, one can certainly cast it as aa = a as any[]
, same as in languages like C# and Java.
为了避免,人们当然可以将其作为aa = a表示为任何[],与C#和Java等语言相同。
Runtime error
运行时错误
JavaScript has no type specification for declaration of variable, so JavaScript engine does not have any idea of about type.
JavaScript没有变量声明的类型规范,因此JavaScript引擎对类型没有任何了解。
Only when you invoke a method or access property, JavaScrip will give you an error..
只有在调用方法或访问属性时,JavaScrip才会给您一个错误。
a:any[] ..
// this will give an error TypeError: .map is not a function
a.map( ()=> ... )
a.length // this is undefined.. any member of any object is
// essentially undefined, there is no type error when you access
// member