我有这个简单的模块,它导出一个返回ChildProcess
实例的函数。问题是我不知道如何添加返回类型信息,因为我不知道如何获得对ChildProcess
类的引用。对TypeScript类型的ChildProcess类的引用
//core
import * as cp from 'child_process';
import * as path from 'path';
//project
const run = path.resolve(__dirname +'/lib/run.sh');
export = function($commands: Array<string>, args?: Array<string>) {
const commands = $commands.map(function(c){
return String(c).trim();
});
return cp.spawn(run, (args || []), {
env: Object.assign({}, process.env, {
GENERIC_SUBSHELL_COMMANDS: commands.join('\n')
})
});
};
如果你看一下Node.js的文档,它说cp.spawn()返回子进程类的一个实例。
如果你看看这里: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/index.d.ts
我们看到的类型定义为子进程类: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/index.d.ts#L1599
不过,我很困惑如何在我的打字稿代码中引用此。
我不认为我预计导入@types/node
,因为这应该是一个devDependency。
我该怎么办?
我需要做的是这样的:
export = function($commands: Array<string>, args?: Array<string>): ChildProcess {
}
得到它,还是真的不知道这是如何工作的,但应该这样做,让我验证 –