0

I'm new to Typescript and I'm declaring arrays like this:

variable1: MyType[];

Then I realized that another guy in my group is declaring them like this:

variable2: [MyType];

I never saw something similiar to the last one in any other language. I asked him what was the difference and he didn't know the answer.

If I try to assign variable1 to variable2, Typescript complains that they are different types and cannot be assigned to each other.

So, my question is, what exactly is the type of variable2?

This may be a pretty stupid/simple question that should be easily found on Google, but I don't even know how to call the variable2 object type to search for it...

1
  • Haskell uses the second form, btw. Fun fact. Commented Dec 29, 2016 at 23:00

1 Answer 1

5
variable1: MyType[];

Is an array of MyType

variable2: [MyType];

is a unary tuple containing a single MyType

The first is a collection, the second is not. If we were using numbers, variable1 would accept values of [n1,n2,...] while variable2 would accept only [n]

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

Comments

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.