Home > Mobile >  TypeScript Array differences
TypeScript Array differences

Time:12-22

I'm Kinda new to TypeScript and was wondering what is the difference between, let array: SomeClass[]; and let array: [SomeClass];? What does the different positions of the brackets mean?

CodePudding user response:

The answer is to do with the amount of instances in each array

someClass[] will mean an array with an infinate length of that class
[someClass] will mean a tuple with a length of one of that class

Ie
someClass[] can be: [someClass], [someClass, someClass] or [someClass, someClass, someClass, someClass] with any amount of values
Whereas [someClass] can only be: [someClass] and no other amount of values in the array

CodePudding user response:

let array: [SomeClass]; defines an array with exaclty one value while let array: SomeClass[]; defines an array with an undefined number of items

  • Related