Typcript2 types: Array <any> and Array <Object>

What is the difference between a field declaration like Array<any>and Array<Object>.

Are there any additional collection tools in typescript?

+4
source share
3 answers

An Objectin TypeScript is the same as in JavaScript.

The difference is that it anyaccepts primitives: a is numbernot an object if it is not placed in square c number.

So, while it Array<any>may contain primitives, Array<Object>it cannot.

TL DR: do not use Object.

Source: TypeScript Do and Do'ts

+3
source

, any Object docs about any:

- JavaScript, . , , . Object - - , , :

let notSure: any = 4;
notSure.ifItExists(); // okay, ifItExists might exist at runtime
notSure.toFixed(); // okay, toFixed exists (but the compiler doesn't check)

let prettySure: Object = 4;
prettySure.toFixed(); // Error: Property 'toFixed' doesn't exist on type 'Object'.
+3

Object , , Object.prototype.

any , .

:

const a: Object = "Object"
a.length // ERROR

const b: any = "Any"
b.length // Compiles
+2

Source: https://habr.com/ru/post/1663627/


All Articles