helion3
helion3

Reputation: 37501

Declare an external module function

I'm trying to declare an external module which has no existing typings, but am missing something.

The library exports a function which takes no arguments and returns a string.

I'm trying to define it using this in a .d.ts file:

declare module "cuid" {
    export function cuid(): string;
}

In my code, I have import * as cuid from 'cuid';

Yet on the line where I use it, cuid() I get an error:

error TS2349: Cannot invoke an expression whose type lacks a call signature.

Upvotes: 9

Views: 6506

Answers (1)

Paleo
Paleo

Reputation: 23772

Use the definition with export function cuid

This syntax matches with your declaration:

import {cuid} from 'cuid';

Here is a good introduction to ES6 modules.

Or use a definition with export =

Try:

declare module "cuid" {
  function cuid(): string;
  export = cuid;
}

... Then use it: import cuid = require('cuid').

Here is the documentation.

Upvotes: 16

Related Questions