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, 2022 in TypeSript by Logan
• 2,140 points
1,173 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, 2022 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, 2022 in TypeSript by Nina
• 3,020 points
84 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, 2022 in TypeSript by Nina
• 3,020 points
83 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, 2022 in TypeSript by Nina
• 3,020 points
2,082 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, 2022 in TypeSript by Nina
• 3,020 points
13,488 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, 2022 in Others by narikkadan
• 50,080 points
1,254 views
0 votes
1 answer

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

Just add this in the app.module.ts file: import { FormsModule ...READ MORE

answered Apr 30, 2022 in Other DevOps Questions by narikkadan
• 50,080 points
1,380 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, 2022 in TypeSript by Nina
• 3,020 points
1,663 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, 2022 in TypeSript by Nina
• 3,020 points
112 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, 2022 in TypeSript by Nina
• 3,020 points
994 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP