本文介绍了VS Code无法识别Typescript内部模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用内部模块将我的打字稿类分隔在单独的文件中.但是,main.ts文件将无法加载或识别子模块.

I am trying to separate my typescript classes in separate files using internal modules. However, the main.ts file will not load or recognize the sub modules.

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",结果相同.

Update: edited the "out" flag in tsconfig to try and compile main.ts and car.ts into everything.js - this is the last part that is not working: everything.js is not created. Instead, VS Code creates a main.js and a car.js. It seems that the "out" flag is ignored. I have also tried "outFile" with the same result.

推荐答案

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


有关模块的更多信息,请不要忘记查看 TypeScript手册

这篇关于VS Code无法识别Typescript内部模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:17
查看更多