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

babel-preset-typescript doesn't work with object casts #6450

Closed
pbadenski opened this Issue Oct 9, 2017 · 10 comments

Comments

Projects
None yet
3 participants
@pbadenski

pbadenski commented Oct 9, 2017

This is a bug.

Current behaviour

For all following (valid) TypeScript babel-typescript-preset throws an error: Unexpected token, expected }.

Input Code

const o = <any> { a: 2 };
const f = () => (<any> {
  property: "value"
});
const f = () => {
  return <any> {
    property: "value"
  };
};

Babel configuration (.babelrc, package.json, cli command)

babel --extensions '.ts' --presets typescript

Your Environment

software version(s)
Babel 7.0.0-beta.2 (babel-core 7.0.0-beta.2)
babel-preset-typescript 7.0.0-beta.2
node v8.4.0
npm 5.3.0
Operating System Mac OS X 10.12.6
@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 9, 2017

Member

Thanks for reporting! looks like a parser specific issue.

@andy-ms was this meant to be supported or is that new?

Member

hzoo commented Oct 9, 2017

Thanks for reporting! looks like a parser specific issue.

@andy-ms was this meant to be supported or is that new?

@andy-ms

This comment has been minimized.

Show comment
Hide comment
@andy-ms

andy-ms Oct 10, 2017

Contributor

Hm, this is working fine for me. Clone https://github.com/andy-ms/babel-typescript-sample and run npm run transpile and it works.

Contributor

andy-ms commented Oct 10, 2017

Hm, this is working fine for me. Clone https://github.com/andy-ms/babel-typescript-sample and run npm run transpile and it works.

@hzoo hzoo added the awaiting reply label Oct 10, 2017

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 14, 2017

Member

Going to close since it doesn't repro?

babel --extensions '.ts' --presets typescript in this command you aren't actually specifying a src or out? babel --extensions '.ts' --presets typescript src -d lib would work?

Member

hzoo commented Oct 14, 2017

Going to close since it doesn't repro?

babel --extensions '.ts' --presets typescript in this command you aren't actually specifying a src or out? babel --extensions '.ts' --presets typescript src -d lib would work?

@hzoo hzoo closed this Oct 14, 2017

@pbadenski

This comment has been minimized.

Show comment
Hide comment
@pbadenski

pbadenski Oct 15, 2017

I did some triangulation and I figured it out, adding "babel-react-env" triggers that issue.. Although I'm not sure which preset causes this exactly. Failing sample here: https://github.com/pbadenski/babel-typescript-sample (based on @andy-ms example, you need to run npm run transpile).

pbadenski commented Oct 15, 2017

I did some triangulation and I figured it out, adding "babel-react-env" triggers that issue.. Although I'm not sure which preset causes this exactly. Failing sample here: https://github.com/pbadenski/babel-typescript-sample (based on @andy-ms example, you need to run npm run transpile).

@andy-ms

This comment has been minimized.

Show comment
Hide comment
@andy-ms

andy-ms Oct 16, 2017

Contributor

@pbadenski npm run transpile works for me there too, although I can't perfectly reproduce your installed package versions due to npm/npm#17979.

Contributor

andy-ms commented Oct 16, 2017

@pbadenski npm run transpile works for me there too, although I can't perfectly reproduce your installed package versions due to npm/npm#17979.

@pbadenski

This comment has been minimized.

Show comment
Hide comment
@pbadenski

pbadenski Oct 18, 2017

I'm not sure what could be wrong then 😞 I just tested and saw this error it on a fresh ubuntu installation. If you could try rm -rf node_modules/ package-lock.json - I did that, and still getting the error. Otherwise I'd have to throw in the towel.

pbadenski commented Oct 18, 2017

I'm not sure what could be wrong then 😞 I just tested and saw this error it on a fresh ubuntu installation. If you could try rm -rf node_modules/ package-lock.json - I did that, and still getting the error. Otherwise I'd have to throw in the towel.

@andy-ms

This comment has been minimized.

Show comment
Hide comment
@andy-ms

andy-ms Oct 18, 2017

Contributor

Hi @pbadenski, I messed up and wasn't actually transpiling anything. (#6502)
Is it possible that "react" is enabling JSX syntax, so that <any> is parsed as an opening tag? You can work around by using { a: 2 } as any instead -- that has equivalent semantics, but a jsx-compatible syntax.

Contributor

andy-ms commented Oct 18, 2017

Hi @pbadenski, I messed up and wasn't actually transpiling anything. (#6502)
Is it possible that "react" is enabling JSX syntax, so that <any> is parsed as an opening tag? You can work around by using { a: 2 } as any instead -- that has equivalent semantics, but a jsx-compatible syntax.

@hzoo hzoo reopened this Oct 19, 2017

@hzoo hzoo removed the awaiting reply label Oct 19, 2017

@pbadenski

This comment has been minimized.

Show comment
Hide comment
@pbadenski

pbadenski Oct 19, 2017

Workaround works great, really good suggestion.

As to your other point, having cursory look at what babylon is doing seems you're right. I know very little about the mechanics of this. Seems like this is an impossible issue to resolve, as TS and JSX syntax are clashing.

pbadenski commented Oct 19, 2017

Workaround works great, really good suggestion.

As to your other point, having cursory look at what babylon is doing seems you're right. I know very little about the mechanics of this. Seems like this is an impossible issue to resolve, as TS and JSX syntax are clashing.

@andy-ms

This comment has been minimized.

Show comment
Hide comment
@andy-ms

andy-ms Oct 19, 2017

Contributor

Yep, that's why we added the as syntax. If you name your files with a .tsx extension, the TS compiler should warn you about invalid cast syntax.

Contributor

andy-ms commented Oct 19, 2017

Yep, that's why we added the as syntax. If you name your files with a .tsx extension, the TS compiler should warn you about invalid cast syntax.

@pbadenski

This comment has been minimized.

Show comment
Hide comment
@pbadenski

pbadenski Oct 19, 2017

Understood. Thanks!

pbadenski commented Oct 19, 2017

Understood. Thanks!

@pbadenski pbadenski closed this Oct 27, 2017

@lock lock bot added the outdated label May 1, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 1, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.