question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

preset-typescript should default to .ts and .tsx

See original GitHub issue

Bug report

  1. I put typescript and javascript files side by side and setup .babelrc config:
{
  "presets": [
    ["@babel/preset-env", {
      "targets": { "electron": "3.0" }
    }],
    "@babel/preset-react",
    ["@babel/preset-typescript", {
      "isTSX": true
    }],
    "@babel/preset-flow"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}
$ babel src/ --copy-files --out-dir build --extensions .ts,.tsx,.js
Successfully compiled 80 files with Babel.

Question

But is there any way to fix the preset-typescript to actually be conscious of file extensions? If Babel parses *.js only by default, is there any way for typescript preset to tell it to pick up .tsx and .ts too? I don’t really want to pass extensions all over my scripts. I believe the defaults should be sane enough.

Also, just tried to compile JSX in .tsx file and it seems to work. But what’s isTSX option is used for then?

isTSX Forcibly enables jsx parsing. Otherwise angle brackets will be treated as typescript’s legacy type assertion var foo = <string>bar;

The part about angle brackets does not make sense. It seems that JSX tags are properly compiled to React.createElement.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
pronebirdcommented, Oct 24, 2018

@loganfsmyth great thanks! I had impression that isTSX is the only way to enable TSX support. But again, doing a practical test, it all seems to work.

Is there any plan to make preset-typescript automatically add .ts and .tsx extensions for parsing by babel? Because I think currently it doesn’t work without --extensions .ts,.tsx.

1reaction
loganfsmythcommented, Oct 24, 2018

But is there any way to fix the preset-typescript to actually be conscious of file extensions?

I think there is some confusion here. That is the default behavior and it sounds like you don’t actually want to set isTSX in the first place. Setting isTSX: true overrides the default and forces it to treat all files as TSX, and for it to do that, you need to set allExtensions.

But what’s isTSX option is used for then?

Assuming you’ve set allExtensions to stop it from auto-deciding based on file extensions, isTSX toggles whether it should treat all files as .ts or .tsx.

It sounds like you want

    ["@babel/preset-env", {
      "targets": { "electron": "3.0" }
    }],
    "@babel/preset-react",
    "@babel/preset-typescript",
    "@babel/preset-flow"
Read more comments on GitHub >

github_iconTop Results From Across the Web

babel/preset-typescript
This preset is recommended if you use [TypeScript](https://www. ... Indicates that every file should be parsed as TS, TSX, or as TS without...
Read more >
Presets | ts-jest - GitHub Pages
ts -jest/presets/default or ts-jest, TypeScript files ( .ts , .tsx ) will be transformed by ts-jest to CommonJS syntax, leaving JavaScript files (...
Read more >
Is there any downside to using .tsx instead of .ts all the times in ...
I believe with the .tsx files you could use the all JSX (JavaScript XML) code. Whereas in the .ts file you can only...
Read more >
TSConfig Reference - Docs on every TSConfig option
... .tsx , and .d.ts by default, with .js and .jsx if allowJs is set to true). ... Setting the value to undefined...
Read more >
babel/preset-typescript
allExtensions. boolean , defaults to false. Indicates that every file should be parsed as TS or TSX (depending on the isTSX ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found