user311413
user311413

Reputation: 177

How to create declaration file for a module which exports function?

I need to create declaration file for a JavaScript module which exports function. The JavaScript module looks like this:

module.exports = hello

function hello() { console.log('Hello') }

I tried creating declaration file different ways:

export function hello(): void
export default function hello(): void
declare function hello(): void

export = hello

Nothing works with any kind of imports:

import hello from './filename'
import { hello } from './filename'

How to create declaration file for such modules?

Upvotes: 0

Views: 570

Answers (1)

Gilson Cavalcanti
Gilson Cavalcanti

Reputation: 1513

What you're dealing with is a Default Export in commonJS:

modules.exports = hello

In your declaration .d.tsfile you shouldn't reimplement the function behavior like you seem to do. You just want to declare the typing for it. I believe this would do it:

declare function hello(): void
export = hello

You also always can use TS Playground in the .D.TS tab to use the feature that TS generates declarations for you: TS Playground screen capture

check more in https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

Upvotes: 3

Related Questions