Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between any and any[ ]?

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 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?.


like image 993
Ramesh Rajendran Avatar asked May 19 '17 11:05

Ramesh Rajendran


People also ask

Where do we use any?

We use any to mean 'it does not matter which or what', to describe something which is not limited. We use this meaning of any with all types of nouns and usually in affirmative sentences.

How do you use any in a sentence?

Adjective any person who comes in the store today is eligible for the discount Adverb The food there is never any good. He won't be any happier there than he was here. I could not walk any farther. I can't eat any more pizza.

Whats the difference between any and some?

The Main Difference Between SOME and ANY As a general rule, we use 'some' for affirmative sentences, and 'any' for questions or negative sentences. Usually, both 'some' and 'any' can only be used with countable plural nouns or uncountable nouns. For example, “I have some questions.”

Which is correct if there is any or if there are any?

In the first, the form "is" agrees with its singular subject. In the second, the form "are" agrees with its plural subject. If there is any scooter or bike parked in front my door, I will throw it away. If there are any scooters or bikes parked in front my door, I will throw them away.


1 Answers

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;

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 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.

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.

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[]'.
like image 58
crashmstr Avatar answered Nov 04 '22 10:11

crashmstr