Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are semicolons necessary in Typescript 2?

Tags:

typescript

I've been searching for an answer to this question for a while and am getting mixed messages. I know semicolons are necessary in JavaScript because of the automatic semicolon insertion (ASI), but does TypeScript have the same restriction?

I would assume that it doesn't, since it transpiles down to JavaScript, and most likely inserts a semicolon for you in the places where the ASI would cause a problem. But I would like to know for sure.

like image 521
Graham Avatar asked Jun 23 '18 19:06

Graham


People also ask

Does JavaScript need semicolons 2022?

JavaScript does not require semicolons (other than the one exception you saw earlier). This is because JavaScript is clever and it can add the semicolons where needed automatically. This happens behind the scenes and you will not notice it. This process is called Automatic Semicolon Insertion (ASI).

Are semicolons necessary?

Semicolons help you connect closely related ideas when a style mark stronger than a comma is needed. By using semicolons effectively, you can make your writing sound more sophisticated.

Is semicolon mandatory in angular?

Firstly, a semicolon is optional only where there is a line break, a closing brace, or the end of the program. Semicolons are not optional between statements appearing on the same line.

Do I need semicolons in JavaScript?

Semicolons are an essential part of JavaScript code. They are read and used by the compiler to distinguish between separate statements so that statements do not leak into other parts of the code.


1 Answers

TypeScript follows the same ASI rules as JavaScript. Semicolons are technically not required in either language, save for a few rare, specific cases. It's best to be educated on ASI regardless of your approach.

Notably, ASI also applies inside of interface and object type bodies:

// valid
interface Person {
  name: string;
  age: number;
}

// also valid
interface Person {
  name: string
  age: number
}

// not valid
interface Person { name: string age: number }
like image 141
kingdaro Avatar answered Oct 25 '22 08:10

kingdaro