Flion
Flion

Reputation: 10902

declaration for a module that exports a self calling function

This should be simple but I can't seem to figure it out. npm module next-tick does something like this in it's index.js:

module.exports = (function () {
    if (/*node*/) {
        return process.nextTick;
    }
    ...
    if (/*other env*/) {
        return function (cb) { setTimeout(callable(cb), 0); };
    }

    return null;
}());

There are no typings yet, so I created next-tick.d.ts and included it in my tsconfig.js.

However I can't figure out what it should contain:

This only thing I got working without compiler errors is this:

declare module "next-tick" {
    export default function(fn:Function)
}

and in the consuming file:

import nextTick from 'next-tick';

But when I bundle with webpack and run it, it says: next_tick_1.default is not a function. So it's trying to call .default.

What also works is if I use require (and add declarations for that with webpack typings)

var nextTick:(fn:Function)=>void = require('next-tick');

But I'm sure I should be able to do this with a typescript import?

Upvotes: 1

Views: 889

Answers (1)

Bruno Grieder
Bruno Grieder

Reputation: 29824

Try this

declare module "next-tick" {
    function nextTick (fn:Function)
    export = nextTick
}

This makes the function to be the export

To import

import nextTick = require('next-tick')

The ES6 import syntax cannot be used because this is just a function, not a module (as required in ES6)

Upvotes: 1

Related Questions