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 #6392

Closed
nojvek opened this Issue Oct 5, 2017 · 27 comments

Comments

Projects
None yet
10 participants
@nojvek

nojvek commented Oct 5, 2017

Followed the documentation at: https://www.npmjs.com/package/babel-preset-typescript

Getting this error:

node node_modules/babel-cli/bin/babel --presets typescript src/models/result.ts
Error: You gave us a visitor for the node type "TSDeclareFunction" but it's not a valid type (While processing preset: "/Users/nojvek/mp/insights/node_modules/babel-preset-typescript/lib/index.js")
    at verify (/Users/nojvek/mp/insights/node_modules/babel-traverse/lib/visitors.js:196:13)
    at Function.explode (/Users/nojvek/mp/insights/node_modules/babel-traverse/lib/visitors.js:72:3)
    at Plugin.normaliseVisitor (/Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/plugin.js:155:29)
    at new Plugin (/Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/plugin.js:66:27)
    at Function.memoisePluginContainer (/Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:119:21)
    at Function.normalisePlugin (/Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:146:32)
    at /Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:184:30
    at Array.map (native)
    at Function.normalisePlugins (/Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:158:20)
    at OptionManager.mergeOptions (/Users/nojvek/mp/insights/node_modules/babel-cli/node_modules/babel-core/lib/transformation/file/options/option-manager.js:234:36)
    "babel-cli": "6.10.1",
    "babel-core": "6.3.17",
    "babel-eslint": "6.0.2",
    "babel-loader": "6.2.0",
    "babel-plugin-syntax-async-functions": "6.13.0",
    "babel-plugin-transform-regenerator": "6.22.0",
    "babel-preset-es2015": "6.3.13",
    "babel-preset-typescript": "^7.0.0-alpha.19",

End goal is to get this working with webpack with babel-loader.

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Oct 5, 2017

Collaborator

Hey @nojvek! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Collaborator

babel-bot commented Oct 5, 2017

Hey @nojvek! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@nojvek

This comment has been minimized.

Show comment
Hide comment
@nojvek

nojvek commented Oct 5, 2017

@existentialism

This comment has been minimized.

Show comment
Hide comment
@existentialism

existentialism Oct 5, 2017

Member

@nojvek thanks for the issue! You'll need to upgrade all your babel deps to the latest betas to match babel-preset-typescript, as it depends on some changes in our parser.

(p.s. no need to ping, the team will respond asap!)

Member

existentialism commented Oct 5, 2017

@nojvek thanks for the issue! You'll need to upgrade all your babel deps to the latest betas to match babel-preset-typescript, as it depends on some changes in our parser.

(p.s. no need to ping, the team will respond asap!)

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 5, 2017

Member

Would appreciate it if you provided the source for the error? nvm, thanks for reporting

It's because you are using the typescript preset when everything else is Babel 6. It's using some APIs only in Babel 7 so you just need to update everything else as well.

I would change everything to 7.0.0-beta.2 (eslint/loader are different)

And the npm version on the website is usually out of date/slow to update*

Member

hzoo commented Oct 5, 2017

Would appreciate it if you provided the source for the error? nvm, thanks for reporting

It's because you are using the typescript preset when everything else is Babel 6. It's using some APIs only in Babel 7 so you just need to update everything else as well.

I would change everything to 7.0.0-beta.2 (eslint/loader are different)

And the npm version on the website is usually out of date/slow to update*

@plmrry

This comment has been minimized.

Show comment
Hide comment
@plmrry

plmrry Oct 5, 2017

I'm getting:

You gave us a visitor for the node type "TSDeclareFunction" but it's not a valid type

Using:

"babel-cli": "7.0.0-beta.2",
"babel-preset-typescript": "7.0.0-beta.2",

plmrry commented Oct 5, 2017

I'm getting:

You gave us a visitor for the node type "TSDeclareFunction" but it's not a valid type

Using:

"babel-cli": "7.0.0-beta.2",
"babel-preset-typescript": "7.0.0-beta.2",
@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 5, 2017

Member

I would clear node_modules and make sure you don't have any other plugins/deps that are using v6

Member

hzoo commented Oct 5, 2017

I would clear node_modules and make sure you don't have any other plugins/deps that are using v6

@nojvek

This comment has been minimized.

Show comment
Hide comment
@nojvek

nojvek Oct 5, 2017

I made the following changes to the package.json

image

webpack.config

var BABEL_LOADER = `babel?presets[]=typescript`;

    loaders: [
      {
        test: /\.(js|ts)$/,
        loader: BABEL_LOADER,
      },

Although I still get an error like this.

ERROR in ./src/index.js
Module parse failed: /Users/nojvek/mp/insights/node_modules/babel-loader/lib/index.js?presets[]=typescript!/Users/nojvek/mp/insights/node_modules/eslint-loader/index.js!/Users/nojvek/mp/insights/src/index.js Line 1: Unexpected token
You may need an appropriate loader to handle this file type.

nojvek commented Oct 5, 2017

I made the following changes to the package.json

image

webpack.config

var BABEL_LOADER = `babel?presets[]=typescript`;

    loaders: [
      {
        test: /\.(js|ts)$/,
        loader: BABEL_LOADER,
      },

Although I still get an error like this.

ERROR in ./src/index.js
Module parse failed: /Users/nojvek/mp/insights/node_modules/babel-loader/lib/index.js?presets[]=typescript!/Users/nojvek/mp/insights/node_modules/eslint-loader/index.js!/Users/nojvek/mp/insights/src/index.js Line 1: Unexpected token
You may need an appropriate loader to handle this file type.
@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 5, 2017

Member

babel-loader can be ^7.1.2 and preset-typescript should also be beta.2

If you only run typescript loader it will probably fail for unknown syntax, you should still run with es2015?

Member

hzoo commented Oct 5, 2017

babel-loader can be ^7.1.2 and preset-typescript should also be beta.2

If you only run typescript loader it will probably fail for unknown syntax, you should still run with es2015?

@hzoo hzoo added the i: question label Oct 7, 2017

@galenjiang

This comment has been minimized.

Show comment
Hide comment
@galenjiang

galenjiang Oct 7, 2017

@hzoo Does this preset only remove type syntax, but it does not run a ts language server for type checking?

galenjiang commented Oct 7, 2017

@hzoo Does this preset only remove type syntax, but it does not run a ts language server for type checking?

@TrySound

This comment has been minimized.

Show comment
Hide comment
@TrySound

TrySound Oct 7, 2017

Contributor

@galenjiang Yes. This is the only reason for new tool.

Contributor

TrySound commented Oct 7, 2017

@galenjiang Yes. This is the only reason for new tool.

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 7, 2017

Member

It works the same as babel-preset-flow. You still need to run flow/ts separately.

Member

hzoo commented Oct 7, 2017

It works the same as babel-preset-flow. You still need to run flow/ts separately.

@babel-bot

This comment has been minimized.

Show comment
Hide comment
@babel-bot

babel-bot Oct 14, 2017

Collaborator

Hi @nojvek! A maintainer of the project has notified me that you're missing
some information we'll need to replicate this issue.

Please understand that we receive a high volume of issues, and there are only a limited number
of volunteers that help maintain this project. The easier it is for us to decipher an issue with the info provided,
the more likely it is that we'll be able to help.

Please make sure you have the following information documented in this ticket:

  1. Your Babel configuration (typically in the form of a .babelrc)
  2. The current (incorrect) behavior you're seeing
  3. The behavior you expect
  4. A short, self-contained example

Please provide either a link to the problem via the repl, or if the repl is insufficient,
a new and minimal repository with instructions on how to build/replicate the issue.

Collaborator

babel-bot commented Oct 14, 2017

Hi @nojvek! A maintainer of the project has notified me that you're missing
some information we'll need to replicate this issue.

Please understand that we receive a high volume of issues, and there are only a limited number
of volunteers that help maintain this project. The easier it is for us to decipher an issue with the info provided,
the more likely it is that we'll be able to help.

Please make sure you have the following information documented in this ticket:

  1. Your Babel configuration (typically in the form of a .babelrc)
  2. The current (incorrect) behavior you're seeing
  3. The behavior you expect
  4. A short, self-contained example

Please provide either a link to the problem via the repl, or if the repl is insufficient,
a new and minimal repository with instructions on how to build/replicate the issue.

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 14, 2017

Member

If you get that it's not a valid node type it's because some package is on v6.
If it still unexpected token it's probably because the syntax after stripping types is a proposal

Either way we don't have the code or a repro so going to close for now from inactivity, can link to a repo or something and we can help

Member

hzoo commented Oct 14, 2017

If you get that it's not a valid node type it's because some package is on v6.
If it still unexpected token it's probably because the syntax after stripping types is a proposal

Either way we don't have the code or a repro so going to close for now from inactivity, can link to a repo or something and we can help

@hzoo hzoo closed this Oct 14, 2017

@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 23, 2017

I am experiencing this exact same problem.

    "babel-cli": "^7.0.0-beta.3",
    "babel-core": "^7.0.0-beta.3",
    "babel-loader": "^7.1.2",
    "babel-preset-typescript": "^7.0.0-alpha.20",
{
  "presets": ["typescript"]
}

rodrigopivi commented Oct 23, 2017

I am experiencing this exact same problem.

    "babel-cli": "^7.0.0-beta.3",
    "babel-core": "^7.0.0-beta.3",
    "babel-loader": "^7.1.2",
    "babel-preset-typescript": "^7.0.0-alpha.20",
{
  "presets": ["typescript"]
}
@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 23, 2017

Member

"babel-preset-typescript": "^7.0.0-alpha.20",

Can you make sure that the version are the same? They should all match (in this case beta.3). Also don't use ^ because each update can be breaking and incompatible with the previous

Member

hzoo commented Oct 23, 2017

"babel-preset-typescript": "^7.0.0-alpha.20",

Can you make sure that the version are the same? They should all match (in this case beta.3). Also don't use ^ because each update can be breaking and incompatible with the previous

@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 23, 2017

thanks for the quick reply. updated to test with this, still not working:

    "babel-cli": "7.0.0-beta.3",
    "babel-core": "7.0.0-beta.3",
    "babel-loader": "7.1.2",
    "babel-preset-typescript": "7.0.0-beta.3",
{
  "presets": ["typescript"]
}

Error:

You gave us a visitor for the node type "TSDeclareFunction" but it's not a valid type (While processing preset: "/XX/node_modules/babel-preset-typescript/lib/index.js")

rodrigopivi commented Oct 23, 2017

thanks for the quick reply. updated to test with this, still not working:

    "babel-cli": "7.0.0-beta.3",
    "babel-core": "7.0.0-beta.3",
    "babel-loader": "7.1.2",
    "babel-preset-typescript": "7.0.0-beta.3",
{
  "presets": ["typescript"]
}

Error:

You gave us a visitor for the node type "TSDeclareFunction" but it's not a valid type (While processing preset: "/XX/node_modules/babel-preset-typescript/lib/index.js")
@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 23, 2017

just using babel works, i get the error with babel-cli

rodrigopivi commented Oct 23, 2017

just using babel works, i get the error with babel-cli

@nicolo-ribaudo

This comment has been minimized.

Show comment
Hide comment
@nicolo-ribaudo

nicolo-ribaudo Oct 23, 2017

Member

Are you using a globally installed babel-cli? Does ./node_modules/.bin/babel ... (instead of babel ...) work?

Member

nicolo-ribaudo commented Oct 23, 2017

Are you using a globally installed babel-cli? Does ./node_modules/.bin/babel ... (instead of babel ...) work?

@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 23, 2017

im using npx babel-cli, so i think now the problem is tsx, it compiles fine, but fails when there is a react tag. any workaround, typescript should have jsx support be default right?

rodrigopivi commented Oct 23, 2017

im using npx babel-cli, so i think now the problem is tsx, it compiles fine, but fails when there is a react tag. any workaround, typescript should have jsx support be default right?

@andy-ms

This comment has been minimized.

Show comment
Hide comment
@andy-ms

andy-ms Oct 23, 2017

Contributor

No, they're separate plugins.

Contributor

andy-ms commented Oct 23, 2017

No, they're separate plugins.

@nojvek

This comment has been minimized.

Show comment
Hide comment
@nojvek

nojvek Oct 23, 2017

nojvek commented Oct 23, 2017

@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 24, 2017

ok after updating to:

    "babel-cli": "7.0.0-beta.3",
    "babel-core": "7.0.0-beta.3",
    "babel-loader": "7.1.2",
    "babel-plugin-transform-react-jsx": "7.0.0-beta.3",
    "babel-preset-env": "^1.6.1",
    "babel-preset-typescript": "7.0.0-beta.3",

I get a new error at:
https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-react-jsx/src/index.js#L5

Uncaught TypeError: Cannot read property 'pragma' of undefined
    at _default (/Users/rodrigo/Trabajo/doc/doc.pe-server/node_modules/babel-plugin-transform-react-jsx/lib/index.js:14:23)
    at Function.memoisePluginContainer (/Users/rodrigo/Trabajo/doc/doc.pe-server/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)

Any ideas?

rodrigopivi commented Oct 24, 2017

ok after updating to:

    "babel-cli": "7.0.0-beta.3",
    "babel-core": "7.0.0-beta.3",
    "babel-loader": "7.1.2",
    "babel-plugin-transform-react-jsx": "7.0.0-beta.3",
    "babel-preset-env": "^1.6.1",
    "babel-preset-typescript": "7.0.0-beta.3",

I get a new error at:
https://github.com/babel/babel/blob/master/packages/babel-plugin-transform-react-jsx/src/index.js#L5

Uncaught TypeError: Cannot read property 'pragma' of undefined
    at _default (/Users/rodrigo/Trabajo/doc/doc.pe-server/node_modules/babel-plugin-transform-react-jsx/lib/index.js:14:23)
    at Function.memoisePluginContainer (/Users/rodrigo/Trabajo/doc/doc.pe-server/node_modules/babel-register/node_modules/babel-core/lib/transformation/file/options/option-manager.js:113:13)

Any ideas?

@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 24, 2017

i was able to get TS work with babel running directly on nodejs, but seems like the TS transpiling still has a lot of errors, so that i was unsuable atm for actual applications. Needed to also add "env" preset, and also plugins/presets for class transformations, decorators, spread operator and jsx.

At the end, i had problems related to exporting types, and also other ts parsing bugs, at the end i hit a wall with a TODO exception that seems like someone left something incomplete. Getting back to TSC for now.

rodrigopivi commented Oct 24, 2017

i was able to get TS work with babel running directly on nodejs, but seems like the TS transpiling still has a lot of errors, so that i was unsuable atm for actual applications. Needed to also add "env" preset, and also plugins/presets for class transformations, decorators, spread operator and jsx.

At the end, i had problems related to exporting types, and also other ts parsing bugs, at the end i hit a wall with a TODO exception that seems like someone left something incomplete. Getting back to TSC for now.

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 24, 2017

Member

@rodrigopivi fyi the ts preset only strips types away like flow, it's expected that you need env and other plugins

Member

hzoo commented Oct 24, 2017

@rodrigopivi fyi the ts preset only strips types away like flow, it's expected that you need env and other plugins

@hzoo

This comment has been minimized.

Show comment
Hide comment
@hzoo

hzoo Oct 24, 2017

Member

If you want help we need a minimal reproduction with code snippet and config, we can't guess how to fix it just from an error message

Member

hzoo commented Oct 24, 2017

If you want help we need a minimal reproduction with code snippet and config, we can't guess how to fix it just from an error message

@rodrigopivi

This comment has been minimized.

Show comment
Hide comment
@rodrigopivi

rodrigopivi Oct 24, 2017

I´m sorry but i lost all the changes i did to my code to try to get it working, but one of the endpoints was this exception thrown:

https://github.com/babel/babel/blob/830c527bb7873b29f671a3568b21b4a6af98ceda/packages/babel-traverse/src/path/conversion.js#L15

This is valid Typescript, that got me an exception at work (so had to coment it out of my code):

export type ISomeType = <T>({ someParam, anotherParam }: IHaveSomeParams<T>) => Promise<T>;

I also posted some screenshots and the steps i went through at the Typescript channel on the babel slack. thanks for being so helpful.

rodrigopivi commented Oct 24, 2017

I´m sorry but i lost all the changes i did to my code to try to get it working, but one of the endpoints was this exception thrown:

https://github.com/babel/babel/blob/830c527bb7873b29f671a3568b21b4a6af98ceda/packages/babel-traverse/src/path/conversion.js#L15

This is valid Typescript, that got me an exception at work (so had to coment it out of my code):

export type ISomeType = <T>({ someParam, anotherParam }: IHaveSomeParams<T>) => Promise<T>;

I also posted some screenshots and the steps i went through at the Typescript channel on the babel slack. thanks for being so helpful.

@andy-ms

This comment has been minimized.

Show comment
Hide comment
@andy-ms
Contributor

andy-ms commented Oct 24, 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.