Skip to content
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

Cannot import TypeScript types that have been re-exported #7882

Closed
jagaapple opened this issue Jul 10, 2019 · 18 comments · Fixed by #13746
Closed

Cannot import TypeScript types that have been re-exported #7882

jagaapple opened this issue Jul 10, 2019 · 18 comments · Fixed by #13746
Labels
examples Issue/PR related to examples

Comments

@jagaapple
Copy link

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.

@kachkaev
Copy link
Contributor

This is the result of having isolatedModules in the autogenerated tsconfig.json and we can't opt-out of that.

I upgraded one rather small app from Next 8 + @zeit/next-typescript to 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 of export { ... } from "./path/to/file" 😱

@Timer Timer added the Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.). label Jul 10, 2019
@Timer
Copy link
Member

Timer commented Jul 10, 2019

This is a Babel bug and I can see how this is frustrating -- we'll want to figure something out for this.

@Timer Timer added this to the 9.0.x milestone Jul 10, 2019
@Timer Timer changed the title Next.js 9 cannot import TypeScript types or interfaces through re-export (index) files Cannot import TypeScript types that have been re-exported Jul 10, 2019
@jeremy-coleman
Copy link

jeremy-coleman commented Jul 11, 2019

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 ..

type P = {
color: 'red'|'green'
}
MyDiv = (props:P) => <div color='red'/>

export {MyDiv, P}

thats not going to work, you have to remove the 'export {MyDiv, P}` and replace with inline exports
like this:

export type P = {
color: 'red'|'green'
}
export MyDiv = (props:P) => <div color='red'/>

also, when you create a barrel file(index.ts), do this:

good
 export * from './MyDiv'
bad
export {MyDiv, P} from './MyDiv'

this codemod will fix most issues
install @codemod/cli and babel-plugin-codemod-named-export-declarations and run this as an npm script

    "trymod": "./node_modules/.bin/codemod -p babel-plugin-codemod-named-export-declarations src/client/**/*.ts"

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:\

@martpie
Copy link
Contributor

martpie commented Jul 11, 2019

This is a Babel bug

Is there a link to this upstream bug?

edit: found it babel/babel#8361 (comment)

@martpie
Copy link
Contributor

martpie commented Jul 11, 2019

To fix it, just replace export { ... } from "./path/to/file" with the export * from "./path/to/file"

babel/babel#8361 (comment)

@jeremy-coleman
Copy link

i put an example repo up here https://github.com/jeremy-coleman/next-tsc-proposal
and a corresponding issue here #7908

@cyrus-za
Copy link

TS 3.8 might help with this isolatedModules issue.
https://devblogs.microsoft.com/typescript/announcing-typescript-3-8/#type-only-imports-exports

@oste
Copy link
Contributor

oste commented Mar 29, 2020

This is a show stopper for my Next project that uses react native web. I am using TS 3.8.3.

@timneutkens
Copy link
Member

@oste TypeScript added import type support for this case as #7882 (comment) provided.

@martpie
Copy link
Contributor

martpie commented Mar 30, 2020

Does Babel TS preset support this syntax yet?

@timneutkens
Copy link
Member

timneutkens commented Mar 30, 2020

As far as I'm aware it does (haven't tried it)

@jagaapple
Copy link
Author

jagaapple commented Apr 1, 2020

Babel 7.9 and Prettier 2.0 support TypeScript 3.8.
Everything will be all right after Next uses them (currently using v7.7).

@bopfer
Copy link

bopfer commented May 31, 2020

Anyone know if there are plans to upgrade Babel to at least 7.9? I searched around, but didn't see any issues or PRs.

I really need it because my monorepo packages use "import type" heavily and right now I can't not import the files I need into my Next app.

@bopfer
Copy link

bopfer commented May 31, 2020

Found a solution for now. Using next-transpile-modules allows me to import from my monorepo packages.

@Timer Timer added examples Issue/PR related to examples good first issue Easy to fix issues, good for newcomers and removed Upstream Related to using Next.js with a third-party dependency. (e.g., React, UI/icon libraries, etc.). good first issue Easy to fix issues, good for newcomers labels Jun 3, 2020
@Timer
Copy link
Member

Timer commented Jun 3, 2020

This can now be accomplished via import type and export type in newer Babel versions.

See: babel/babel#11171

@Timer
Copy link
Member

Timer commented Jun 4, 2020

Example landing in #13746 which will close this!

@kodiakhq kodiakhq bot closed this as completed in #13746 Jun 4, 2020
kodiakhq bot pushed a commit that referenced this issue Jun 4, 2020
Closes [7882](#7882).
Created as requested by @timneutkens 

I'm unsure if that's exactly what you wanted, so let me know what you want me to change and I'll do it asap.
@adamsoffer
Copy link
Contributor

Hey guys - heads up just tried deploying the with-typescript-types example and still getting the '=' expected. error.

rokinsky pushed a commit to rokinsky/next.js that referenced this issue Jul 11, 2020
Closes [7882](vercel#7882).
Created as requested by @timneutkens 

I'm unsure if that's exactly what you wanted, so let me know what you want me to change and I'll do it asap.
@Timer Timer removed this from the backlog milestone Nov 16, 2020
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
examples Issue/PR related to examples
Projects
None yet
Development

Successfully merging a pull request may close this issue.