Reputation: 8136
I'm trying to write a typings file for changeset. I'm keeping it internal to my own project (this may be relevant later). Changeset has two use cases:
import * as changeset from "changeset";
changeset({}, {}); // call as function
changeset.applyChanges({}, {}); // call a property on changeset
the actual function in
changeset
is calledapply
, notapplyChanges
. I've changed it here to avoid confusion with Javascript's built inapply
method.
To accomplish this I've set up the above file (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, changeset(...)
does not:
This expression is not callable.
Type 'typeof import("changeset")' has no call signatures.
My .d.ts
is very similar to TypeScript's suggestion, only missing export = changeset;
at the bottom. I don't fully understand why, but adding the export causes the import in index.ts
to error out:
Could not find a declaration file for module 'changeset'.
As far as I can guess, the difference is that other .d.ts
files are imported from node_modules
whereas this one is implicately part of my project.
Is there any way to include complex module declarations like this in a single project?
EDIT
Tried a different way:
declare module "changeset" {
const changeset: {
(objA: any, objB: any): any;
applyChanges: (changes: any, obj: any) => any;
};
export = changeset;
}
Same error as before: changeset.applyChanges(...)
works correctly, changeset(...)
does not...
Upvotes: 0
Views: 200
Reputation: 186984
I think your type is fine, but if the changeset
module has a default export (the main function, with properties on it that are also functions), you probably want to import it like so:
import changeset from "changeset";
Upvotes: 1