0

I have some experience with TypeScript, but one thing just keeps playing on my mind. I know the difference between Array<string> and string[]. I know that the declarations can be used interchangeably, f.e.

export class SomeClass {

   someDeclaration: Array<SomeObject>;
   otherDeclaration: SomeObject[];

}

But in my work, I faced other declaration structure, namely:

export class OtherClass {

   strangeDeclaration: [SomeObject];

}

My question is: Is it correct way to declaring array? Which difference is beetwen this way and other (most popoular) ways? Where does the structure come from?

1

1 Answer 1

4

TypeScript arrays can be written Array<T> or T[] as you suggested.

The other type is a "Tuple". In TS this translated to a index typed array. E.g. it's a array with a fixed type at the given position.

Example 'tuple array': [Number, String]

Ts Docs explains this very well

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.