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?
-
Does this answer your question? Difference between type[] and [type] in TypeScriptJo Liss– Jo Liss2024-03-22 17:40:06 +00:00Commented Mar 22, 2024 at 17:40
2 Answers
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
Comments
let array: [SomeClass]; defines an array with exaclty one value while let array: SomeClass[]; defines an array with an undefined number of items