2016-05-02 39 views
0

WebワーカーとNodeで実行したいスクリプトにQというライブラリを使用しています。ので、。ノードとWebワーカー向けのJSのファイル

error TS2300: Duplicate identifier 'Q'. 

が、私はimport Q = require("q")var Q = require("q")に置き換えた場合、私はこのエラーを取得する:私はそうのようなq.d.tsを参照する場合:

/// <reference path="../typings/q/Q.d.ts" /> 

if(importScripts != undefined && WorkerGlobalScope == undefined 
     && typeof module !== 'undefined' && module.exports){ 

    // We're in a Node (child) process 
    var Q = require("q") 
}else{ 
    // We're in a web-worker 
    importScripts('q'); 
} 

私がうまくこのanswerに説明されている次のエラーを取得しますimportステートメントは、if句の中に入れ子になっており、スクリプトのトップレベルには入れられません。

Qを入力して、ノードのrequire()とウェブ作業者のimportScripts()との両方で動作するようにする方法はありますか?

答えて

1

Is there a way to require Q in so that it works with both node's require() and web-worker's importScripts()?

あなたはQのちょうど種類をインポートしてを必要に応じてqなまけをロードする必要があります。だから、:

import _Q = require('q'); 
if(importScripts != undefined && WorkerGlobalScope == undefined 
     && typeof module !== 'undefined' && module.exports){ 

    // we're in a Node (child) process 
    var Q:typeof _Q = require("q") 
}else{ 

    importScripts('q'); 
} 

もっと

これは、ここでは遅延ロードのセクションで説明されています `.d.ts`ファイルには、モジュールを宣言するときhttps://basarat.gitbooks.io/typescript/content/docs/project/external-modules.html

+0

これは素晴らしい作品が、それらのためにそのドン」 ( 'require.d.ts'など)(https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/q/Qdts)' import _r = require( 'requirejs') 'がこのようになりますエラー: 'モジュール 'requirejs'を見つけることができません。 – Jthorpe

+0

私は[別の関連する質問](http://stackoverflow.com/questions/38016793)があります。ありがとう! – Jthorpe

関連する問題