akibo
akibo

Reputation: 715

typescript object type doesn't show error for omitting semi-colon

I found a strange thing with object type declaration. I expect the p2 is the correct one, but it worked with a , or even I omit the semi-colon, it's still fine. Why is it so inconsistent?

let p: {
    x: number,
    y: string
} = {
    x: 1,
    y: "abc",
}


let p2: {
    x: number;
    y: string
} = {
    x: 1,
    y: "abc",
}


let p3: {
    x: number
    y: string
} = {
    x: 1,
 ...

Playground Link

Upvotes: 1

Views: 285

Answers (1)

Arpan Banerjee
Arpan Banerjee

Reputation: 1016

Semicolons in JavaScript are optional due to Automatic Semicolon Insertion (ASI). TypeScript follows ASI, too. ASI is not straightforward and there are a few situations where omitting a semicolon will lead to an unexpected runtime error. But the few corner cases in JavaScript are further eliminated by TypeScript’s type system.

// valid
class Employee{
  name: string;
  age: number;
}

// also valid
class Employee{
  name: string
  age: number
}

Here is a link of a similar question that might help you-- Are semicolons necessary in typescript?

Upvotes: 2

Related Questions