Reputation: 1186
I am trying to create a type definition for a module that replaces module.exports with an anonymous function. So, the module code does this:
module.exports = function(foo) { /* some code */}
To use the module in JavaScript (Node) we do this:
const theModule = require("theModule");
theModule("foo");
I've written a .d.ts file that does this:
export function theModule(foo: string): string;
I can then write a TypeScript file like this:
import {theModule} from "theModule";
theModule("foo");
When I transpile into JavaScript, I get:
const theModule_1 = require("theModule");
theModule_1.theModule("foo");
I am not the module author. So, I can not change the module code.
How do I write my type definition so that it transpiles correctly to:
const theModule = require("theModule");
theModule("foo");
EDIT: For clarity, based upon the correct answer, my final code looks like this:
the-module.d.ts
declare module "theModule" {
function main(foo: string): string;
export = main;
}
the-module-test.ts
import theModule = require("theModule");
theModule("foo");
which will transpile to the-module-test.js
const theModule = require("theModule");
theModule("foo");
Upvotes: 2
Views: 218
Reputation: 20159
For Node-style modules that export a function, use export =
function theModule(foo: string): string;
export = theModule;
Upvotes: 2