Max Heiber
Max Heiber

Reputation: 15582

How can I import a CommonJS module that exports a single function from TypeScript

Given this CommonJS module:

// cjs.js
module.exports = () => console.log("hi");

What can I put in my .d.ts

// cjs.d.ts
export ????

So that I can do a star import like this from a TypeScript file and pick up the correct types:

// main.ts
import * as log from "./cjs"

log()

Upvotes: 1

Views: 284

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 250226

You can use export assignment, but you need to declare an intermediary const that will be typed as the function:

declare const def: () => void
export = def;

Upvotes: 1

Related Questions