Skip to content

Fails to compile "x = 2 as number" when "as" starts a new line. #24284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arielshaqed opened this issue May 21, 2018 · 4 comments
Closed

Fails to compile "x = 2 as number" when "as" starts a new line. #24284

arielshaqed opened this issue May 21, 2018 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@arielshaqed
Copy link

arielshaqed commented May 21, 2018

TypeScript Version: typescript v2.9.0-dev.20180519

(Also with v2.8.3)

Search Terms:

"as keyword" newline
"as keyword" whitespace

Code

Fails:

const x = 2
as number;
console.log(x);

Works:

const x = 2 as
number;
console.log(x);

Expected behavior:
Successful compilation (and output "2"...).

Actual behavior:
tsnode says:

...(2,4): ';' expected. (1005)

Playground puts wiggly red lines on everything, starting on first as.

Playground Link: https://www.typescriptlang.org/play/#src=const%20x%20%3D%202%0D%0A%20%20as%20number%3B%0D%0Aconsole.log(x)%3B%0D%0A

Related Issues:
Couldn't find something similar (see search terms above). It looks related to the general weirdness associated with newlines in JavaScript. The playground seems to support this, it shows this translation:

x = 2;
as;
number;
console.log(x);
@j-oliveras
Copy link
Contributor

This is because automatic semicolon insertion, so this is intended. The sample is parsed as:

const x = 2;
as number;
console.log(x);

but because as and number are not expected typescript shows errors (and try to do it's better to parse them).

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 21, 2018
@RyanCavanaugh
Copy link
Member

Might be ASI concerns here, but I think we can safely parse an "as " from the next line?

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented May 21, 2018

No, this is the intended behavior:

@DanielRosenwasser DanielRosenwasser added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Bug A bug in TypeScript labels May 21, 2018
@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants