Skip to content

Ternary with computed property object emits invalid JS #12588

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
blacktaxi opened this issue Nov 30, 2016 · 2 comments
Closed

Ternary with computed property object emits invalid JS #12588

blacktaxi opened this issue Nov 30, 2016 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@blacktaxi
Copy link

TypeScript Version: 2.1.1

Code

const x = true ? { ['test']: 5 } : {};

Expected behavior:
TS 2.0.6 emits this JS:

var x = true ? (_a = {}, _a['test'] = 5, _a) : {};
var _a;

Actual behavior:
TS 2.1.1 emits this JS:

var x = true ? _a = {}, _a['test'] = 5, _a : {};
var _a;

The problem with it is that it's not a valid JS syntactically. Here's the output from node 5.9.0 from attempt at running it:

[stdin]:1
var x = true ? _a = {}, _a['test'] = 5, _a : {};
                      ^

SyntaxError: Unexpected token ,
    at Object.exports.runInThisContext (vm.js:53:16)
    at Object.<anonymous> ([stdin]-wrapper:6:22)
    at Module._compile (module.js:413:34)
    at node.js:550:27
    at _combinedTickCallback (node.js:376:9)
    at process._tickCallback (node.js:407:11)
@mhegazy
Copy link
Contributor

mhegazy commented Nov 30, 2016

Looks fixed in latest. can you give typescript@next a try.

c:\test>type a.ts
const x = true ? { ['test']: 5 } : {};

c:\test>tsc --v
Version 2.2.0-dev.20161130

c:\test>tsc a.ts

c:\test>type a.js
var x = true ? (_a = {}, _a['test'] = 5, _a) : {};
var _a;

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label Nov 30, 2016
@mhegazy mhegazy added this to the TypeScript 2.1.3 milestone Nov 30, 2016
@mhegazy mhegazy closed this as completed Nov 30, 2016
@vladima vladima added the Duplicate An existing issue was already created label Nov 30, 2016
@vladima
Copy link
Contributor

vladima commented Nov 30, 2016

duplicate of #12413

@vladima vladima removed the Fixed A PR has been merged for this issue label Nov 30, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 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

3 participants