typescript-go icon indicating copy to clipboard operation
typescript-go copied to clipboard

Cannot import from .d.ts files

Open lukpsaxo opened this issue 8 months ago • 1 comments

We try and import a type:

Image

the file is a .d.ts

Image

and it exports what we want to import:

Image

tsc does this fine but tsgo gives an error:

utils/forgeRock/middlewares.ts:5:8 - error TS2307: Cannot find module '@forgerock/javascript-sdk/dist/config/interfaces' or its corresponding type declarations.

5 } from '@forgerock/javascript-sdk/dist/config/interfaces';
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

lukpsaxo avatar May 30 '25 06:05 lukpsaxo

im getting this same problem i think with react-instantsearch package, latest ver

im trying

import {Hits} from 'react-instantsearch';
Hits;

and getting

main.tsx:1:20 - error TS2307: Cannot find module 'react-instantsearch' or its corresponding type declarations.

1 import {Hits} from 'react-instantsearch';
                     ~~~~~~~~~~~~~~~~~~~~~

Found 1 error in main.tsx:1

tsc no errors

elee1766 avatar May 30 '25 19:05 elee1766

moduleResolution: node is removed and it's internally remapped to moduleResolution: bundler (I got this info from @jakebailey). This means that package.json#exports are always interpreted when they are present in package.json and this field is meant to expose everything from the package that is meant to be exposed. Everything else is considered private and inaccessible.

@lukpsaxo @forgerock/javascript-sdk doesnt expose dist in its exports

@mattlewis92 Similarly, libphonenumber-js doesn't expose types in its exports

Both of those can fail with moduleResolution: node16 (and newer values too) in Strada

@elee1766 I can't repro any issue with react-instantsearch, could you recheck if you are still affected by the recent preview builds?

Andarist avatar Jun 19 '25 18:06 Andarist

Is there any work around ?

It’s going to be chaos I the ecosystem asking authors to re export types from their index everywhere.

I will raise issues for the packages where it affects us.

lukeapage avatar Jun 19 '25 18:06 lukeapage

Sorry it seems most of our cases they were exposed somehow already e.g. forgerock sdk exports src/* -> dist/* so changing the imports to a src file made it work, it just takes some digging :)

lukpsaxo avatar Jun 19 '25 18:06 lukpsaxo

@mattlewis92 Similarly, libphonenumber-js doesn't expose types in its exports

Ah I see, I did try setting moduleResolution: "bundler" but tsc still compiled OK. After a bit of trial and error I found a typeRoots: ["node_modules"] entry in the tsconfig that was somehow making it compile with tsc 🤷‍♂ Removing that, and I now get consistent errors between tsc and tsgo 🥳

mattlewis92 avatar Jun 19 '25 19:06 mattlewis92

So, it seems like everything is actually working correctly? (Where correctly means "node10 resolution is unsupported, use bundler"?)

jakebailey avatar Jun 20 '25 16:06 jakebailey

@jakebailey yes, happy to close, though it might avoid some noise to print a error “falling back to bundler module resolution which might cause some import errors, see xyz”

lukeapage avatar Jun 20 '25 16:06 lukeapage

Yes, we generally speaking are missing all of the "program errors" which complain about deprecated / bad options. This would be one thing we should report. Though, I think by the time we get there, it might be too late to realize we've remapped the options...

jakebailey avatar Jun 20 '25 16:06 jakebailey

Filed #982.

jakebailey avatar Jun 20 '25 16:06 jakebailey