我假設以下目錄結構
├── lib
│ ├── jslib.d.ts
│ └── jslib.js
├── src
└── t.ts
jslib.js
var jslib = jslib || (function() {
var publicMethods = {
encript: function() {
return
}
};
return publicMethods;
})();
module.exports = jslib;
jslib.d.ts
declare namespace jslib {
function encript(): void;
}
export = jslib;
t.ts
import jslib = require('../lib/jslib');
jslib.encript();
檢查與node_modules/.bin/tsc --traceResolution
======== Resolving module '../lib/jslib' from '/home/zjk/dev/webnote/ts1/src/t.ts'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module as file/folder, candidate module location '/home/zjk/dev/webnote/ts1/lib/jslib'.
File '/home/zjk/dev/webnote/ts1/lib/jslib.ts' does not exist.
File '/home/zjk/dev/webnote/ts1/lib/jslib.tsx' does not exist.
File '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts' exist - use it as a name resolution result.
Resolving real path for '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts', result '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts'
======== Module name '../lib/jslib' was successfully resolved to '/home/zjk/dev/webnote/ts1/lib/jslib.d.ts'. ========
關鍵是要放jslib.js
和jslib.d.ts
在同一目錄下。
來源
2016-10-11 02:42:39
zjk
謝謝zjk。我有兩個愚蠢的問題1)在JavaScript中,如何理解var jslib = jslib ||函數(),我不明白爲什麼jslib需要在「=」2)之後再次出現在你的答案中,const jslib是什麼意思?對不起,我對這兩個腳本都是綠色的.. –
@SkyLine。好問題!我不是專家。我會在我的回答中更新 – zjk
我還沒有試過我的答案。我稍後再展開它。至於'var jslib = jslib ||函數()',它可以防止重新定義整個jslib,如果它已經定義。 – zjk