VS Code无法识别Typescript内部模块 [英] Typescript internal module not recognized by VS Code

查看:297
本文介绍了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

Kokodoko :我终于找到了问题!您必须忽略"tasks.json"中的"args"选项,然后才将其中的参数 tsconfig.json可以使用!我在这里找到了答案: github.com/Microsoft/typescript/wiki/tsconfig.json.它说:什么时候 输入文件是在命令行上指定的,tsconfig.json文件是 已忽略

Kokodoko: I finally found the problem! You have to OMIT the "args" option inside "tasks.json", only then will the arguments in tsconfig.json be used! I found the answer here: github.com/Microsoft/typescript/wiki/tsconfig.json. It says: When input files are specified on the command line, tsconfig.json files are ignored


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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆