Robert Brax
Robert Brax

Reputation: 7318

Type declaration file and call signature error

Due to the lack of official d.ts file for a slug module, I get all kinds of error trying it to work.

First, here is the file where I import the module.

import * as slug from "slug" // Had to create a custom d.ts empty file to avoid compile error as no official definition file existed

let slugify = function (data: string): string {
    let slugified: string = slug('ééé aaa ooo') // error here

    return slugified

}

export default slugify

And here is the empty d.ts file i had to create to prevent import error message from the compiler:

declare module "slug" {

}

new problem is on slug('ééé aaa ooo') :

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

How can I resolve this ? Should I add "fake" content in the d.ts file ?

Upvotes: 1

Views: 932

Answers (1)

David Sherret
David Sherret

Reputation: 106640

The compiler needs to know how slug works and you can let it know by exporting a declaration of that function—something that represents how slug looks in JavaScript—from the module in the definition file.

Here's one way of doing it:

declare module "slug" {
    interface SlugOptions {
        lower?: boolean;
        // ...add the other options here...
    }

    function slug(val: string, optionsOrReplacement?: SlugOptions | string): string;

    export = slug;
}

I believe the only way to import this library is to use require since it doesn't use a named/default export in the js code:

import slug = require("slug");

Upvotes: 1

Related Questions