2017-02-20 51 views
2

我有这个简单的模块,它导出一个返回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 { 

} 

回答

3

看起来ChildProcesschild_process模块下,所以你应该能够与您现有的进口引用它:

import * as cp from 'child_process'; 

export = function($commands: Array<string>, args?: Array<string>): cp.ChildProcess { 
    //... 
} 
+0

得到它,还是真的不知道这是如何工作的,但应该这样做,让我验证 –

1

对我来说它努力改变

import { spawn } from 'child_process'; 

import { ChildProcess, spawn } from 'child_process'; 

这摆脱了错误的:

error TS4023: Exported variable 'readGitTags' has or is using name 'ChildProcess' from external module "child_process" but cannot be named.