Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a custom node.js addon module using TypeScript

I have a custom Node.js addon that I've written, but I'd like to use typescript. I can use bindings to import the module in JavaScript

const addon = require('bindings')('addon');

which will import addon.node from build/release/

How can I do this using TypeScript? Does it allow addon import?

like image 592
Giallo Avatar asked Nov 08 '22 10:11

Giallo


1 Answers

You should be able to create a declaration file that adds type information for your addon module, and then import it using a relative path in TypeScript.

For good examples of declaration files for external modules, see the DefinitelyTyped GitHub repository. A succinct good example from that directory is the declaration for the camelcase NPM nodule:

declare module "camelcase" {
    function camelcase(...args: string[]): string;
    namespace camelcase {}
    export = camelcase;
}
like image 116
Zac B Avatar answered Nov 14 '22 22:11

Zac B