最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

对TypeScript类型的ChildProcess类的引用

IT培训 admin 6浏览 0评论

对TypeScript类型的ChildProcess类的引用

我有这个简单的模块,它导出一个返回ChildProcess实例的函数。问题是我不知道如何添加返回类型信息,因为我不知道如何获取对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()返回一个ChildProcess类的实例。

如果你看这里:.d.ts

我们看到ChildProcess类的类型定义:.d.ts#L1599

但是,我很困惑如何在我的TypeScript代码中引用它。

我不认为我应该导入@types/node,因为这应该是一个devDependency。

我应该做些什么?

我需要做类似的事情:

export = function($commands: Array<string>, args?: Array<string>): ChildProcess {

}
回答如下:

它看起来像ChildProcesschild_process模块下,所以你应该能够用你现有的导入引用它:

import * as cp from 'child_process';

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

对TypeScript类型的ChildProcess类的引用

我有这个简单的模块,它导出一个返回ChildProcess实例的函数。问题是我不知道如何添加返回类型信息,因为我不知道如何获取对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()返回一个ChildProcess类的实例。

如果你看这里:.d.ts

我们看到ChildProcess类的类型定义:.d.ts#L1599

但是,我很困惑如何在我的TypeScript代码中引用它。

我不认为我应该导入@types/node,因为这应该是一个devDependency。

我应该做些什么?

我需要做类似的事情:

export = function($commands: Array<string>, args?: Array<string>): ChildProcess {

}
回答如下:

它看起来像ChildProcesschild_process模块下,所以你应该能够用你现有的导入引用它:

import * as cp from 'child_process';

export = function($commands: Array<string>, args?: Array<string>): cp.ChildProcess {
  //...
}
发布评论

评论列表 (0)

  1. 暂无评论