-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Types are broken with compilerOptions.module
set to nodenext
due to imports missing file extensions
#9890
Comments
This issue is blocking a big update I'm working on for |
We were able to resolve this same issue for AS4 via apollographql/apollo-server#6731 |
What is the result of this. Is there a fix in the client library or will it ? |
@GeenCo86 The outcome of that PR is that this same issue was resolved for Apollo Server in our recent v4 release. Prioritizing and implementing a fix for this in Apollo Client is up to the @apollographql/client-typescript team. I've just provided the PR as additional context. It's likely they'd be interested in reviewing a PR which provably (i.e. with tests) resolves the issue without breaking existing use cases! |
Any updates on this? This is causing us quite a bit of pain as we try to move from CJS to ESM, and it seems like a pretty simple fix for the Apollo client... |
would be great if you guys could provide an update here. thank you in advance! |
@phryneas would you mind weighing in here? I'm not sure if this type of change would be considered breaking or whether we can do a minor here. |
I think it might have broader implications, like us also needing to ship I'm gonna set some time aside to experiment with that. @markerikson, I would be very thankful if you could chime in here. |
What specific change are you considering here? FWIW, I think this is why I've opted to bundle all of our artifacts for the Redux libs, so I don't have to worry about file extensions inside of transpiled sources (ie, |
@markerikson Unfortunately, exactly that one - concatenating all imports with |
The problem that we have is here:
it worked before when we used CJS, but after refactor to ESM now it says:
And this happens with all interfaces, hooks, etc that definitely exported from To fix that we do
and
but some of the types that used inside these functions are still broken, but Typescript is not complaining because it happens inside module itself |
Never bundle modules that are published, that's a big anti-pattern. It forces users to import and load everything in your library regardless if they are using it or not. Packages with multiple public exports should never have an index module; users should deep import only the export they need from a specific module that has a single export. You can learn more about optimal module design here: https://jaydenseric.com/blog/optimal-javascript-module-design |
@jaydenseric we've been shipping bundles for most of our libraries for years, and I'm especially doing that now that we're trying to ship full ESM/CJS compat to avoid other pain points. Tree shaking seems to work fine with Vite/ESBuild, Next/Webpack, etc. Just last night I was converting React-Redux to have bundled artifacts instead of separate transpiled JS files on disk, and confirmed that a Vite app correctly tree-shakes Note that we assume that almost all of our users are building apps using a bundler, not importing raw ESM into a browser. Along with that, we have embedded (FWIW, I just finished reading your article, and while I appreciate the amount of time you put into writing it, I generally disagree with most of the points being made and don't intend to re-change the packaging setups I've just put months into updating.) |
@jaydenseric @vtereshyn could you give |
@phryneas A little late to the party here but super appreciative for tackling this issue 🙏 . In the midst of converting my org's libraries to ESM and this was definitely an unexpected gotcha. Can confirm /cc @jaydenseric |
Ah right, I forgot to close this after we shipped 3.8 - good call, closing :) |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Intended outcome:
Projects with TypeScript config
compilerOptions.module
set tonodenext
ornode16
should be able to import@apollo/client
modules with working types. This is critical for using TypeScript correctly with modern Node.js packages or projects that use the packageexports
field, ESM in.mjs
files, etc.Actual outcome:
Types are broken for
@apollo/client
modules that have types that import other modules/types without correctly specifying the file extensions in the imports.How to reproduce the issue:
In
package.json
:In
jsconfig.json
:In
demo.mjs
:Notice how the types are incomplete for the
ApolloLink
constructor arguments and fall back toany
:If you dig into the types for it, you can see that the
RequestHandler
type is being imported incorrectly (missing the file extension) from./types
, and TypeScript is unable to tell what its type is:If you change the import path to
./types.js
(add the missing file extension), the types start to work:Ultimately fixing the types where
ApolloLink
is being constructed in the project:All imports, including for type only imports, should specify the full path including the file name for the module being imported.
As a side note, type only imports should use the
type
keyword but currently they don't:apollo-client/src/link/core/ApolloLink.ts
Lines 4 to 10 in 289336a
E.g:
Versions
typescript
: v4.7.4@apollo/client
: v3.6.9The text was updated successfully, but these errors were encountered: