I am in the process of creating a typings file for a library called changeset within my project. This library has two main use cases:
import * as changeset from "changeset";
changeset({}, {}); // used as a function
changeset.applyChanges({}, {}); // calling a property on changeset
The actual function in
changeset
is namedapply
, notapplyChanges
. I made this adjustment to prevent confusion with JavaScript's built-inapply
method.
To achieve this, I have created the following files: index.ts
and changeset.d.ts
declare function changeset(objA: any, objB: any): any;
declare module "changeset" {
export function applyChanges(changes: any, obj: any): any;
}
changeset.applyChanges(...)
works correctly, but changeset(...)
does not:
This expression is not callable.
Type 'typeof import("changeset")' has no call signatures.
My .d.ts
closely resembles TypeScript's recommended structure, except it lacks export = changeset;
at the end. Adding this causes an error in the import statement of index.ts
:
Could not find a declaration file for module 'changeset'.
From what I can gather, the difference lies in other .d.ts
files being imported from node_modules
, while this one is specific to my project.
Is there a way to properly include complex module declarations like this within a single project?
EDIT
Attempted a different approach:
declare module "changeset" {
const changeset: {
(objA: any, objB: any): any;
applyChanges: (changes: any, obj: any) => any;
};
export = changeset;
}
Encountered the same issue as before: changeset.applyChanges(...)
functions correctly, while changeset(...)
does not...