Pregunta

I'm trying to write a Typescript decleration file for the Yargs js library.

How do I write the ambient type decleration for an external module with the dual behaviour ...

require('yargs').argv

and ...

require('yargs')([ '-x', '1', '-y', '2' ]).argv
¿Fue útil?

Solución

declare module "foo" {
    function M(argArray: any[]): typeof M;
    module M {
        export var argv;
    }

    export = M;
}

Otros consejos

This one was tricky. It took some thinking.

declare module yargs {
    export interface Yargs {
        argv: string[];
        (input: string[]): Yargs;
        count(option: string): Yargs;
        alias(shrt: string, lng: string): Yargs;
    }
}

declare module "yargs" {
    var y: yargs.Yargs;
    export = y;
}

Simply export a function as the exported module.

For example:

// foo.js

function foo(args) {
  return {
    get: function() { return args; }
    size: args.length
  };
};

foo.bar = 'bar';

module.exports = foo;

// Example node usage.
require('./foo.js').bar // => 'bar'
f = require('./foo.js')([1,2,3]);
f.get(); // => [1,2,3]
f.size; // => 3
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top