对 TypeScript 类型的 ChildProcess class 的引用
Reference to ChildProcess class for TypeScript types
我有这个简单的模块,它导出一个 return 是 ChildProcess
实例的函数。问题是我不知道如何添加 return 类型信息,因为我不知道如何获取对 ChildProcess
class.
的引用
//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() returns 是 ChildProcess class.
的一个实例
如果你看这里:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/index.d.ts
我们看到了 ChildProcess 的类型定义 class:
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/index.d.ts#L1599
但是,我对如何在我的 TypeScript 代码中引用它感到困惑。
我不认为我应该导入 @types/node
因为这应该是一个 devDependency。
我该怎么办?
我需要做类似的事情:
export = function($commands: Array<string>, args?: Array<string>): ChildProcess {
}
看起来 ChildProcess
在 child_process
模块下,因此您应该可以使用现有的导入来引用它:
import * as cp from 'child_process';
export = function($commands: Array<string>, args?: Array<string>): cp.ChildProcess {
//...
}
对我来说它改变了
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.
我有这个简单的模块,它导出一个 return 是 ChildProcess
实例的函数。问题是我不知道如何添加 return 类型信息,因为我不知道如何获取对 ChildProcess
class.
//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() returns 是 ChildProcess class.
的一个实例如果你看这里: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/index.d.ts
我们看到了 ChildProcess 的类型定义 class: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/node/index.d.ts#L1599
但是,我对如何在我的 TypeScript 代码中引用它感到困惑。
我不认为我应该导入 @types/node
因为这应该是一个 devDependency。
我该怎么办?
我需要做类似的事情:
export = function($commands: Array<string>, args?: Array<string>): ChildProcess {
}
看起来 ChildProcess
在 child_process
模块下,因此您应该可以使用现有的导入来引用它:
import * as cp from 'child_process';
export = function($commands: Array<string>, args?: Array<string>): cp.ChildProcess {
//...
}
对我来说它改变了
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.