9

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.

Graham
  • 5,488
  • 13
  • 57
  • 92
  • Example: http://www.typescriptlang.org/play/#src=function%20foo()%3A%20number%20%7B%0D%0A%20%20%20%20return%0D%0A%20%20%20%204%3B%0D%0A%7D – Caramiriel Jun 23 '18 at 19:04
  • 1
    Possible duplicate of [When to use a semicolon in TypeScript?](https://stackoverflow.com/questions/38823062/when-to-use-a-semicolon-in-typescript) – kingdaro Jun 23 '18 at 19:19

1 Answers1

14

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 }
kingdaro
  • 11,528
  • 3
  • 34
  • 38