'How to reference type existing declaration file at different path?

We are consuming a node package, which provides both CommonJS and ESM exports as well as types.

Due to reasons, we need to import the module by pointing at the ESM directly. This works, however TypeScript can no longer find the type declaration on the new path.

// import { SomePackage } from 'somepackage' // <- unfortunately not an option right now
import { SomePackage } from 'somepackage/dist/somepackage.esm'

Could not find a declaration file for module 'somepackage/dist/somepackage.esm'.
'somepackage/dist/somepackage.esm' implicitly has an 'any' type.
If the 'somepackage' package actually exposes this module, try adding a new declaration (.d.ts) file containing `declare module 'somepackage/dist/somepackage.esm';

I have tried (as the type error suggests) to create a new type declaration file.

declare module 'somepackage/esm/somepackage.esm'

This will get rid of the error, but now all of the types for this package are imported as any.

How do I configure TypeScript to use the type declaration file exposed by the package even if the import path is different?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source