2017-02-20 19 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()はChildProcessクラスのインスタンスを返します。

あなたがここを見ている場合: 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であるはずです。

私は何をすべきですか?

のように、私は何かをする必要があり

:私にとって

import * as cp from 'child_process'; 

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

答えて

3

それは変更するために働いた

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.

+0

が、これがどのように動作するか本当にわからない、まだ、それを得たが、それはそれを行う必要があり、私は確認してみましょう –

1

ChildProcesschild_processモジュールの下にあるので、あなたがあなたの既存のインポートとそれを参照することができるはずのように見えます

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

} 
関連する問題