babel-loader: Typescript: Unable to re-export a type with Webpack 4
I’m submitting a bug report Webpack Version: 4.4.1 Babel Core Version: 7.0.0-beta.44 Babel Loader Version: 8.0.0-beta.0 Please tell us about your environment:
I am using Bable beta with the typescript preset.
In one file, I define and export a type:
// a.ts
export type MyType = "a" | "b";
In another file, I re-export this type:
// b.ts
export { MyType } from "./a.ts";
Current behavior:
The export line is not present when a.ts
is compiled using Babel. The re-export line in b.ts
is compiled to:
export { MyType } from "./b";
Webpack fails with this error:
ERROR in ./app/client/utils.ts
79:0-51 "export 'myType' was not found in './a'
@ ./b.ts
Expected/desired behavior:
It worked fine with Webpack 3, I am not sure why.
babel
should not output anything for the re-export but it does. I am not sure why Webpack 3 was not complaining about the issue, but now Webpack 4 does.
I am not sure if this should be filled in this repo, or with Webpack or Babel core. I can provide a repro if this is needed and the correct place for this issue.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 23
- Comments: 17 (3 by maintainers)
You can still do are-export if it’s clear that you’re exporting a type:
You can also do
export * from "./a";
.@niksumeiko, that cool, but not supported by babel-loader;
export type { IGeocoder } from './types';
crush build withUnexpected token
errorThis is also a problem you would encounter with
ts-loader
if you were using the--isolatedModules
option, which emits code without knowledge of the types of other modules (types only used for checking, not emit).The babel typescript transform works pretty much just like tsc’s transform with
--isolatedModules
enabled.Webpack 4 now complains about the error because this checking was added but disabled by default in webpack 3; it’s just enabled by default in webpack 4.
A way of reexporting that works on both babel and typescript
--isolatedModules
would be this:but tsservice doesn’t seem to treat the reexport the same as the original for purposes of go-to-definition and find usages.
export * from './a'
seems to be workingThis issue will be fixed in Typescript 3.8. They already have a merged PR microsoft/TypeScript#35200 that will introduce
import type
,export type
keywords.A new syntax for type-only imports and exports will look as follows:
https://devblogs.microsoft.com/typescript/announcing-typescript-3-8-beta/
The last comment here by @Akiyamka addresses an issue I’ve ran into lately. I still get an
Unexpected token
error. Is this syntax supported or not?@SapienTech to be more exact, to get this to work I had to specifically update to
"@babel/preset-typescript": "^7.12.7",
@donaldpipowitch I’ve also had success (at least with my config) with the following, no
as
needed:edit: I see this was already mentioned in an earlier comment, that’s probably where I got the idea 😃
@petmat I had same issue, and updating to latest babel fixed it for me.