1

I prefer coding style without semicolons. The following snippet gives me an error:

let x = 123
(window as any).test = 'hello'

The error is:

Cannot invoke an expression whose type lacks a call signature. Type 'Number' has no compatible call signatures.

If I put a semicolon at the end of the first line it compiles without an error.

I thought that a newline character should be enough for TS to parse those statements separately. Maybe I don't know something, maybe it's a bug? I'd like to know the explanation, please.

EDIT: In the accepted answer from the provided link it's stated that ASI occurs in case of var statement. Which makes me think that the same applies to let and const.

So, we have a var statement and a LineTerminator and according to this TS should parse these two lines as two statements, not one. Where am I wrong?

  • 1
    https://stackoverflow.com/q/2846283/6680611 – cartant Jan 02 '18 at 15:12
  • 1
    Possible duplicate of [What are the rules for JavaScript's automatic semicolon insertion (ASI)?](https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) - try the same thing in JavaScript (without the `as any`, of course) and you'll get the same result. – Joe Clay Jan 02 '18 at 15:25
  • @JoeClay, I've updated the question. – Ry Kotovark Jan 02 '18 at 15:53

1 Answers1

1

A quote from the ECMAScript spec, with my emphasis added:

When, as the program is parsed from left to right, a token (called the offending token) is encountered that is not allowed by any production of the grammar, then a semicolon is automatically inserted before the offending token if one or more of the following conditions is true...

An expression such as 123 followed by a set of parentheses is an allowed production of the grammar - it's a function call!

The parser sees 123( and thinks you're trying to call 123 as a function, so it throws an error. This is hinted at by the message you get - it's saying that Number doesn't have a type definition for when you call it.

Joe Clay
  • 26,622
  • 4
  • 68
  • 75