Typescript add custom type definitions for esm library from commonjs project

194 Views Asked by At

When using commonjs libraries it is possible to extend/overwrite their type, for example:

declare module 'ramda' {
  function path<T>(path: Path, obj: any): T;
}

But how can we do this with esm libraries? I am trying to achieve same thing with @keycloak/keycloak-admin-client:

declare module '@keycloak/keycloak-admin-client' {
  function getNum(): number;
}

But I am getting the following error:

The current file is a CommonJS module whose imports will produce 'require' calls;
however, the referenced file is an ECMAScript module and cannot be imported with 'require'.
Consider writing a dynamic 'import("@keycloak/keycloak-admin-client")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts',
or add the field `"type": "module"` to '/workspaces/project-name/backend/package.json'.

I am not ready to migrate my entire project to esm yet. So how can I declare types for esm external libraries?

0

There are 0 best solutions below