commonjs - How to write a typescript definition file for a node module that exports a function? -
consider, toml node module can use:
// toml.d.ts declare module toml { export function parse(value:string):any; } declare module "toml" { export = toml; }
then:
/// <reference path="../../../../../defs/toml/toml.d.ts"/> import toml = require('toml'); toml.parse(...);
however, node module export single function, such 'glob' (https://github.com/isaacs/node-glob).
the node usage of module be:
var glob = require("glob") glob("*.js", {}, callback(err, files) { ... });
you'd naively expect this:
// glob.d.ts declare function globs(paths:string, options:any, callback:{(err:any, files:string[]):void;
...but because typescripts 'import' semantics bit strange, seems can use 'import .. = require()' statement alias modules. trying call:
/// <reference path="../../../../../defs/glob/glob.d.ts"/> import blog = require('glob');
results in:
error ts2072: module cannot aliased non-module type.
so, how write definition file this?
nb. notice commonjs module using node, not amd module.
...also yes, know can breaking type system using declare, i'm trying avoid that:
declare var require; var glob = require('glob'); glob(...);
use export =
.
definition:
// glob.d.ts declare module 'glob' { function globs(paths: string, options: any, callback: (err: any, files: string[]) => void); export = globs; }
usage:
/// <reference path="glob.d.ts"/> import glob = require('glob'); glob("*.js", {}, (err, files) => { });
Comments
Post a Comment