我正试图使用内部模块将typescript类分离到不同的文件中。但是,main.ts文件将不会加载或识别子模块。
main.ts
/// <reference path="Car.ts" />
module Vehicles {
var c = new Vehicles.Car("red");
}
car.ts
module Vehicles {
export class Car {
color: string;
constructor(color: string) {
this.color = color;
console.log("created a new " + color + " car");
}
}
}
tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"out": "everything.js main.ts car.ts"
}
}
更新:编辑tsconfig中的"out"标志,尝试将main.ts和car.ts编译为everything.js-这是最后一个不起作用的部分:没有创建everything..js。相反,VS Code创建了一个main.js和一个car.js。似乎忽略了"out"标志。我也尝试过"outFile",得到了同样的结果。
main.ts
/// <reference path="car.ts" />
var c = new Car("red");
car.ts
class Car {
color: string;
constructor(color: string) {
this.color = color;
console.log("created a new " + color + " car");
}
}
tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"outFile": "main.js"
},
"files": [
"main.ts",
"car.ts"
]
}
tasks.json
Kokodoko:我终于发现了问题!您必须省略"tasks.json"中的"args"选项,只有这样使用tsconfig.json!我在这里找到了答案:github.com/Microsoft/typescript/wiki/tsconfig.json。上面写着:输入文件是在命令行中指定的,tsconfig.json文件是忽略
有关模块的更多信息,请不要忘记查看TypeScript手册
要使用VS Code任务将多个.ts文件编译成一个大的.js文件,需要从tasks.json中删除"args",并将"out"参数添加到tsconfig.json 中
tasks.json
{
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"showOutput": "silent",
"problemMatcher": "$tsc"
}
tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"out": "myapp.js"
}
}
注:当在命令行上指定输入文件时,tsconfig.json文件将被忽略。