Reputation: 45
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?
Upvotes: 3
Views: 695
Reputation: 35797
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.
Upvotes: 2