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.
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).
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.
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.
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.
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With