Write a declaration file for a default export module

TypescriptTypescript Typings

Typescript Problem Overview


I have a npm module called RiveScript that usually (in Javascript) gets instantiated that way:

var RiveScript = require('rivescript');
var rivescript = new RiveScript();

I'm trying to write a declaration file for the module, but am stuck at the first step. Here's what I've written so far:

declare module "rivescript" {
	
	interface RivescriptOptions {
		utf8?: boolean;
	}
	
	class RiveScript {
		constructor(options?: RivescriptOptions);
	}
	
	export default RiveScript;
}

Then I guess in Typescript I would be using the module this way (default import):

import RiveScript from 'rivescript';
let rivescript = new RiveScript();

However, tsc generates this, which is not valid as it references a default() function:

const rivescript_1 = require('rivescript');
let rivescript = new rivescript_1.default();

What am I doing wrong?

Typescript Solutions


Solution 1 - Typescript

You're really close. Instead of using export default, you should use export =.

custom-typings/rivescript.d.ts

declare module 'rivescript' {
  class RiveScript {
    constructor()
  }
  export = RiveScript
}

app.js

import RiveScript = require('rivescript');
let rivescript = new RiveScript();

For more info on how to write declaration files, you should have a look at the Typescript Handbook. Eg. they have a template for 'exporting modules as a class.

Solution 2 - Typescript

In case you were looking for a solution applicable to default exported functions:

declare module "my-module" {
  function myFunction(foo: number): string;

  export default myFunction;
}

Solution 3 - Typescript

Accepted anwser works well for this question. But I want to give another idea

if you want to extends class, add dynamic method to class at runtime.

you can try (src/plugins/processor.ts)

    import server from '../../server'
    
    declare module "../../server"{
        export default interface server{
            process() // it's a new method of server
        }
    }

    export default class{ //another codes, just for show I create a 'server' instance
        private instance:server
        constructor{
            this.instance = new server()
            this.instance.process() //works
        }
    }

in my server.ts(src/server.ts),

my default export class signature

    export default class Server extends extend implements Startable<void>

Because Server is a default export, so you can change server in export default interface server to any valid variable name

for example:

export default interface anotherName    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionjulien_cView Question on Stackoverflow
Solution 1 - TypescriptPelle JacobsView Answer on Stackoverflow
Solution 2 - TypescriptPaul Razvan BergView Answer on Stackoverflow
Solution 3 - TypescriptnuclearView Answer on Stackoverflow