Square brackets around vs after expression in Typescript

0 votes

Is there a difference between the following two square bracket notations in Typescript? Tried a couple of scenarios and it seems they are equivalent?

interface test {
  a: string;
  b: string;
}

const x: test[] = [{a: "aaaa", b: "bbbb"}]

const y: [test] = [{a: "aaaa", b: "bbbb"}]
Jun 3 in TypeSript by Logan
• 2,140 points
138 views

1 answer to this question.

0 votes

x is an Array, while y is a Tuple.

The difference is observable even in this simple case.

interface test {
  a: string;
  b: string;
}

const x: test[] = [{a: "aaaa", b: "bbbb"}]
const y: [test] = [{a: "aaaa", b: "bbbb"}]

x.push({a: "a1", b: "b1"});  // works fine
y.push({a: "a1", b: "b1"});  // works fine

const a = x[1]; // works fine 
const b = y[1]; // compilation error 
                // Tuple type '[test]' of length '1' has no element at index '1'.
answered Jun 7 by Nina
• 3,020 points

Related Questions In TypeSript

0 votes
1 answer

How to create ES6 Map in Typescript

Refer this as an example this.configs = new ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
24 views
0 votes
1 answer

What is TypeScript and why would I use it in place of JavaScript?

TypeScript is a superset of JavaScript which primarily ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
13 views
0 votes
1 answer

How to declare and initialize a Dictionary in Typescript

Apparently this doesn't work when passing the ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
220 views
0 votes
1 answer

What is "not assignable to parameter of type never" error in TypeScript?

All you have to do is define ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
853 views
0 votes
1 answer

How to set meta tags using Angular universal SSR and ngx-seo plug-in?

first Install the plug-in with npm i ngx-seo ...READ MORE

answered Feb 11 in Others by narikkadan
• 11,280 points
724 views
0 votes
0 answers

Can't bind to 'ngModel' since it isn't a known property of 'input'

I have this  input in my component ...READ MORE

Apr 29 in Other DevOps Questions by Kichu
• 19,040 points
149 views
0 votes
1 answer

VS Code enforces semicolons, but why doesn't Angular and TypeScript not use it consistently?

JavaScript has a syntactic feature known as semicolon ...READ MORE

answered May 31 in TypeSript by Nina
• 3,020 points
52 views
0 votes
1 answer

What does the @ (at sign) sign do in TypeScript?

The big news this week is the ...READ MORE

answered Jun 10 in TypeSript by Nina
• 3,020 points
25 views
0 votes
1 answer

What do square brackets mean in a type definition in Typescript?

I've missed a basic type of TypeScript: Tuples. So ...READ MORE

answered Jun 22 in TypeSript by Nina
• 3,020 points
75 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP