私はビデオチュートリアルを見ており、私は新しいタイスクリプトプロジェクトを作成しました。 まず、私はルートディレクトリに作成された次の名前空間(utilityFunctions.ts):Typescriptプロジェクトで名前空間を正しくインポートするにはどうすればいいですか?
/// <reference path="utilityFunctions.ts" />
let fee: number = Utility.Fees.CalculateLateFee(10);
console.log(`Fee: ${fee}`);
:
namespace Utility {
export namespace Fees {
export function CalculateLateFee(daysLate: number): number {
return daysLate * .25;
}
}
export function MaxBooksAllowed(age: number): number {
if (age < 12){
return 3;
}
else {
return 10;
}
}
//function that is not exported
//use it only inside the namespace
function privateFunc(): void {
console.log('This is private...');
}
}
は、それから私は別のtypescriptファイル(app.ts)上記の名前空間のコードを使用することを作成しました次
/Users/Administrator/.nvm/versions/node/v6.5.0/bin/node /Users/Administrator/WebstormProjects/NamespaceDemo/app.js
/Users/Administrator/WebstormProjects/NamespaceDemo/app.js:5
var fee = Utility.Fees.CalculateLateFee(10);
^
ReferenceError: Utility is not defined
at Object.<anonymous> (/Users/Administrator/WebstormProjects/NamespaceDemo/app.js:5:24)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)
at Module.runMain (module.js:590:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
Process finished with exit code 1
マイtsconfig.jsonファイルされる:私は、私は次のエラーを取得しています(webstorm最新バージョンを使用して)app.jsファイルを実行
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": true
},
"exclude": [
"node_modules"
]
}
と(私はリンターは、コンパイル・エラーとは何か持っていることはないと思うが)私のtslint.jsonファイルは以下の通りです:JavaScriptに
{
"extends": "tslint:recommended",
"rules": {
"comment-format": [false, "check-space"],
"eofline": false,
"triple-equals": [false, "allow-null-check"],
"no-trailing-whitespace": false,
"one-line": false,
"no-empty": false,
"typedef-whitespace": false,
"whitespace": false,
"radix": false,
"no-consecutive-blank-lines": false,
"no-console": false,
"typedef": [true,
"variable-declaration",
"call-signature",
"parameter",
"property-declaration",
"member-variable-declaration"
],
"quotemark": false,
"one-variable-per-declaration": false,
"max-line-length": 160,
"object-literal-sort-keys": false,
"trailing-comma": false,
"variable-name": [true,
"ban-keywords",
"check-format",
"allow-leading-underscore"
]
}
}
のようなインライン参照コードを生成します。ファイルが見つからないことがあります。私はスクリプトタグがどのように解決されるかについては何も決定していませんが、tsconfigにこのコンパイラオプション "moduleResolution"を追加する必要があるかもしれません。 "node"わかりませんが、スクリプトタグ解決にも影響するかもしれません。 –
Ok.Mystery解決!私はちょうどビデオの残りの部分を見て、男はまったく同じエラーを取得しました。それはnodejsの問題です.NodeJSは複数のファイルの名前空間を受け入れていません(モジュールだけを受け取ります - ) "outFile"という行を私のtsconfig.jsonに "out.js"という行を追加して、すべてのプロジェクトのtypescriptファイルを1つのjavascriptファイルにまとめるこの単一のファイルを実行しなければなりませんでした。 – skiabox