在我使用的是所謂的第三方庫bunyan像這樣我的打字稿代碼:打字稿:避免需要在報表編譯的JavaScript
private logger: bunyan.Logger = bunyan.createLogger({name: "MyClass"});
因爲打字稿無法解析變量bunyan
,我這樣做是爲了使打字稿編譯器的工作:
import * as bunyan from "bunyan";
不幸的是這會導致下面的JavaScript輸出:
var bunyan = require("bunyan");
require
語句在瀏覽器中不起作用(當不使用requirejs實現時),因此我將收到:Uncaught ReferenceError: require is not defined
。
其實我不需要require
聲明在我編譯的JavaScript中,因爲我可以使用瀏覽器的bunyan.min.js
(browserified版本)。但是如何避免在我的TypeScript代碼中輸入bunyan而不讓TypeScript編譯器抱怨未知引用?
我用的打字稿1.8,這是我的打字稿編譯器配置:
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
"noImplicitAny": false,
"removeComments": true,
"target": "es5"
},
"exclude": [
"node_modules",
"typings/browser",
"typings/browser.d.ts"
]
}
感謝您的快速回答。但是如果我這樣做,我會得到'錯誤TS2503:找不到命名空間'bunyan''。 –
我的錯誤@BennyNeugebauer。我已經更新了我的答案:) –
謝謝,詹姆斯!我會試試看!我真的需要將bunyan的每個方法聲明爲接口方法,還是可以使用它的'index.d.ts'文件? –