declare module "foo" {
function M(argArray: any[]): typeof M;
module M {
export var argv;
}
export = M;
}
Typescript typing: Function with properties
-
09-07-2023 - |
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
Solución
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