preset-typescript should default to .ts and .tsx
See original GitHub issueBug report
- 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:
- Created 5 years ago
- Reactions:7
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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
.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. SettingisTSX: true
overrides the default and forces it to treat all files as TSX, and for it to do that, you need to setallExtensions
.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