next.js: Cannot import TypeScript types that have been re-exported
Bug report
Describe the bug
As I wrote an issue in @zeit/next-typescript some months ago, Babel does not support to import types or interfaces from re-export files such as index.ts .
On large project, developers create index.js (or index.ts ) to encapsulate the API calls from the web app into a single folder.
@zeit/next-typescript throws warnings when importing re-exported types, but Next.js 9 (using built-in TypeScript transpile system) throws errors in that case. So we cannot develop using re-export files.
To Reproduce
// packages/package-1.ts
export type TypeA = {};
// packages/index.ts
export { TypeA } from "./package-1";
// main.ts
import { TypeA } from "./packages";
Next.js throws errors like the following if the above code is contained in some pages.
Attempted import error: 'TypeA' is not exported from './package-1'.
Expected behavior
Do not raise any errors and warnings.
Additional context
Babel v7 supports to transpile TypeScript files but it is only to remove types from TypeScript files. At this time, the target of import from intersection (index) files will be removed.
// After transpile by Babel...
// ---
// packages/package-1.ts
// export type TypeA = {}; // REMOVED!
// packages/index.ts
export { TypeA } from "./package-1"; // NOT FOUND!
To solve this issue at its foundation, Babel should support re-export files and we should wait to do. However I think that to filter and ignore these errors using regular expressions makes to solve.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 17
- Comments: 18 (12 by maintainers)
Commits related to this issue
- [Example] with-typescript-types (#13746) Closes [7882](https://github.com/vercel/next.js/issues/7882). Created as requested by @timneutkens I'm unsure if that's exactly what you wanted, so let me k... — committed to vercel/next.js by todortotev 6 years ago
- [Example] with-typescript-types (#13746) Closes [7882](https://github.com/vercel/next.js/issues/7882). Created as requested by @timneutkens I'm unsure if that's exactly what you wanted, so let me k... — committed to rokinsky/next.js by todortotev 6 years ago
This is a Babel bug and I can see how this is frustrating – we’ll want to figure something out for this.
TS 3.8 might help with this isolatedModules issue. https://devblogs.microsoft.com/typescript/announcing-typescript-3-8/#type-only-imports-exports
Babel 7.9 and Prettier 2.0 support TypeScript 3.8. Everything will be all right after Next uses them (currently using v7.7).
This is the result of having
isolatedModulesin the autogeneratedtsconfig.jsonand we can’t opt-out of that.I upgraded one rather small app from Next 8 +
@zeit/next-typescriptto Next 9 and it was quite painful to refactor all the re-exports. The resulting working code pretty ugly too. Can’t imagine upgrading a proper app that has hundreds ofexport { ... } from "./path/to/file"😱Hey guys - heads up just tried deploying the
with-typescript-typesexample and still getting the'=' expected.error.Found a solution for now. Using next-transpile-modules allows me to import from my monorepo packages.
As far as I’m aware it does (haven’t tried it)
although the error isn’t clear, its root cause is from exporting a named type, not really from re exporting. this occurs with ts-loader when using transpile only, rollup plugin typescript, etc. (any tool that uses the ts.transpile api). So, if you have a component file like …
thats not going to work, you have to remove the 'export {MyDiv, P}` and replace with inline exports like this:
also, when you create a barrel file(index.ts), do this:
this codemod will fix most issues install @codemod/cli and babel-plugin-codemod-named-export-declarations and run this as an npm script
other options: 2) add ts-loader with enforce pre 3)move all your code out of your next folder and use the typescript compiler to compile it back into where it was. it’s pretty hacky but fast AF if you use the tsc incremental option 4)create a workspace with all your types in it
i tried to do some real hackery using gulp to expose the compiled vinyl files over a socket and serve that to next (just to avoid writes to disk). it worked but couldn’t get updates to trigger:\