0

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?

1

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

It's not an array with one value, it's a tuple
A tuple type is another sort of Array type that knows exactly how many elements it contains, and exactly which types it contains at specific positions. Its the same thing. The a tuple by Definition just knows the amount of values in an array. So an array that always has one entry is a tuple and the other way around. JS dosn't support true tupels so every tupel in JS is an array
A tuple is a different concept entirely. It's a TypeScript happens to realise it through arrays but that doesn't mean an array and duple are the same thing outside of TS.
Yes but as I said JS does not support Tupels only arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.