2017-07-31 94 views
0

的設置:
我創建了一個打字稿文件(String.ts)來擴展字符串:打字稿:無法找到參照編譯時

interface StringConstructor { 
    isNullOrEmpty(str: string): boolean; 
    isNullOrWhiteSpace(str: string): boolean; 
} 

String.isNullOrEmpty = (str: string) => !str; 
String.isNullOrWhiteSpace = (str: string) => { return (String.isNullOrEmpty(str)) ? false : str.replace(/\s/g, '').length < 1 }; 

,我創建的tsconfig.json文件包含:

{ 
    "compilerOptions": { 
    "noImplicitAny": false, 
    "strict": true, 
    "noEmitOnError": true, 
    "module": "amd", 
    "removeComments": true, 
    "target": "es5", 
    "declaration": true, 
    "outFile": "./Common.js", --> Only this line changes in other tsconfig.json files (+ does not exist in highest located tsconfig.json file) 
    "inlineSourceMap": true, 
    "inlineSources": true, 
    "lib": [ 
     "dom", 
     "es5", 
     "es2015.promise" 
    ] 
    }, 
    "exclude": [ 
    "node_modules", 
    "wwwroot" 
    ] 
} 

文件結構:

  • 通用(文件夾)
    〜String.ts
    〜<其他文件>
    〜tsconfig.json
  • Bussines(文件夾)
    〜app.ts - >這裏我們使用String.IsNullOrWhiteSpace
    〜<其他文件>
    〜tsconfig.json
  • tsconfig.json

問題(按以下順序)
1)當我有一個tsconfig.json(最高級別),一切OK編譯
2)當我在公共添加tsconfig.json,一切OK編譯
3)當我在Business中添加tsconfig.json時,出現無法找到String.isNullOrWhiteSpace的編譯錯誤。

任何人都知道問題是什麼以及如何解決它?

(如果需要更多的信息.​​..讓我知道!)

+0

爲什麼你需要多個'tsconfigs'? – Wernerson

+0

例如,將Common文件夾中的所有文件編譯爲一個文件並提高性能。通過將它們分割成多個文件,我仍然保持良好的可維護性代碼 – RubenHerman

+0

我也喜歡將我的接口分割成單獨的文件,但是當我們編譯這個時,我們得到一個空的* .js文件 - >因此編譯一個文件中的所有內容解決了這個問題。但是,如果我們將所有內容編譯到這個文件(最高級別),那麼在其他應用程序中也可以重複使用它。 – RubenHerman

回答

0

String.ts不包括在編譯時分離的業務目錄(即其將需要從其他地方進口)。你應該有一個單獨的通用目錄,稱爲接口和通用定義。

+0

我可以在我的tsconfig.json中使用「include」來解決這個問題嗎? (如果是這樣,怎麼樣?) – RubenHerman

+0

使用'import'即'import'../ Common/String'' –