首页 > 解决方案 > 使用 Visual Studio Code 设置打字稿的问题

问题描述

背景

我是 nodejs/typescript/VisualStudio Code 的新手,并试图使用 express & sequelize-typescript 创建一个小项目。我用 Visual Studio(不是代码)开始了这个项目,但是遇到了很多错误错误或没有出​​现错误的问题,所以我切换到了 Visual Studio Code。我不再确定 Visual Studio(非代码)是否自行安装了 nodejs 或/以及我是否安装了一个额外的。但我知道我在 PATH 变量中有一个 npm。

ts-nameof

然后在这个项目中,我需要做一个查询来使用 order by,所以不是硬编码列名或使用安全字符串名称(我讨厌字符串!)我发现了 ts-nameof。

所以我按照说明进行操作:

当事情变得疯狂时

“node_modules/ts-nameof/ts-nameof.d.ts”

在 tsconfig.json“文件”属性中。是的,它有效!

ReferenceError: nameof 未定义

但运行时不喜欢它......

其他用户的奖励:

可能是因为您使用的是仅适用于 linux/osx 的 nodes_modules/npm/ttsc(我不知道它是否可以在带有 linux 嵌入式设备的 Windows 10 上运行)

对你不利的任务

顺便说一句:我对这个文件还有其他问题,请参阅 tasks.json 注释的底部...

问题/问题

我在使用 Visual Studio(不是代码)搜索 typescript/nodejs 时遇到问题,因为每个人都在使用 Visual Studio 代码......所以我这边肯定有问题。

文件

.vscode/tasks.json:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "windows": {
        "command": "node_modules\\.bin\\ttsc.cmd",
        "type": "shell"
    },
    "tasks": [
        {
            //"label": "ttsc: watch",
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "option": "watch",
            "problemMatcher": [
                "$tsc-watch"
            ]
        },
        {
            //"label": "ttsc: build",
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "problemMatcher": [
                "$tsc"
            ]
        }
    ],
    "problemMatchers": [
        "$tsc"
    ]
}

Visual Studio Code 似乎与这个文件无关,从文档中我应该能够在“任务”对象中使用命令/类型(我认为是“windows”),它不会让我离开。

此外,当使用此文件运行我的任务时,Visual Studio Code 会询问我问题匹配器并将此属性添加到 tasks.json 的根目录中。

编辑:我也尝试在任何地方填充命令/类型属性(在根目录中,在我的所有任务中,在“窗口”中(在根目录和我的所有任务中)。

.vscode/settings.json(工作区)

{
    "files.exclude": {
        "node_modules": true,

        // visual studio project
        "obj": true,
        "bin": true
    },

    // ts-nameof
    "typescript.tsdk": "node_modules/ttypescript/lib"
}

我确实在用户设置中添加了“typescript.tsdk”以防万一。但我可以在这个领域写任何东西,没关系我永远不会出错。

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "sourceMap": true,
    "noImplicitAny": true,
    "outDir": "dist",
    "baseUrl": "./dist/",
    "plugins": [{ "transform": "ts-nameof", "type": "raw" }],

    // for sequelize-typescript models
    "experimentalDecorators": true,
    // for sequelize-typescript
    "emitDecoratorMetadata": true
  },
  // Also part of experimentalDecorators
  "include": [
    "."
  ],
  "exclude": [
    "node_modules",

    // VisualStudio project
    "obj",
    "bin"
  ],
  "files": [ "node_modules/ts-nameof/ts-nameof.d.ts" ]
}

标签: typescriptvisual-studio-codevscode-settings

解决方案


I still have no clue of what going on but the tasks.json syntax I read about seem to be the basic one and languages extend it hence why type could by typescript and contains a property like tsconfig. Unfortunately I didn't find the syntax for typescript.

I end up creating the shell tasks like so :

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "ttsc: watch",
            "type": "shell",
            "windows": {
                "command": "node_modules\\.bin\\ttsc.cmd",
                "args": [
                    "--watch", "-p", "tsconfig.json"
                ],
            },
            "problemMatcher": [
                "$tsc-watch"
            ]
        },
        {
            "label": "ttsc: build",
            "type": "shell",
            "windows": {
                "command": "node_modules\\.bin\\ttsc.cmd",
                "args": [
                    "-p", "tsconfig.json"
                ],
            },
            "problemMatcher": [
                "$tsc"
            ]
        }
    ]
}

推荐阅读