Skip to content
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

Ternary operator parse error (TS 2.5) #18204

Closed
alexanderby opened this issue Sep 1, 2017 · 4 comments
Closed

Ternary operator parse error (TS 2.5) #18204

alexanderby opened this issue Sep 1, 2017 · 4 comments
Assignees
Labels
Duplicate An existing issue was already created

Comments

@alexanderby
Copy link

TypeScript Version: 2.5.2

Code

> tsc script.js --allowJs --outFile result.js

var xMapper = isXPeriod ?
    (createPeriodCaster(props.x.period)) :
    (function (x) {
        return x;
    });

Expected behavior:

Should compile OK

Actual behavior:

script.js(2,24): error TS1005: '=' expected.
script.js(3,5): error TS8010: 'types' can only be used in a .ts file.
script.js(3,19): error TS1005: ')' expected.
script.js(5,6): error TS1005: ':' expected.

TypeScript 2.4.1 worked OK.

@alexanderby
Copy link
Author

Another example of errored file https://github.com/d3/d3-interpolate/blob/master/src/string.js

@j-oliveras
Copy link
Contributor

Duplicated/related to #16241 (see my last comment).

Tested, and your sample works with 2.4.2 and not 2.5.2.

Seems like the compiler has some ambiguity errors with ternary operator with both (ts ans js).

@mhegazy mhegazy added the Bug A bug in TypeScript label Sep 5, 2017
@mhegazy mhegazy added this to the TypeScript 2.5.3 milestone Sep 5, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Sep 6, 2017

Possibly related to #18123

@sandersn sandersn changed the title Ternary (conditional) operator error in JS files (TS 2.5) Ternary operator parse error (TS 2.5) Sep 7, 2017
@sandersn
Copy link
Member

sandersn commented Sep 7, 2017

Thanks @j-oliveras for the pointer. This is indeed a dupe of #16241 — it now manifests with the function keyword because function(number): void is parsed as a (JSDoc) type since 2.4.

The workaround is to not parenthesize the true branch of the ternary, because this makes the parser think that it could be looking at the start of an arrow function.

@sandersn sandersn closed this as completed Sep 7, 2017
@mhegazy mhegazy added Fixed A PR has been merged for this issue Duplicate An existing issue was already created and removed Bug A bug in TypeScript Fixed A PR has been merged for this issue labels Sep 7, 2017
@mhegazy mhegazy removed this from the TypeScript 2.5.3 milestone Sep 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants