Background
我是nodejs / typescript / VisualStudio Code的新手,并且正在尝试使用express和sequelize-typescript创建一个小项目 . 我使用Visual Studio(非代码)启动了该项目,但是出现了很多错误或没有出现错误的问题,所以我切换到了Visual Studio Code . 我不确定Visual Studio(不是代码)是自己安装了nodejs还是/如果我安装了另外一个 . 但我知道我在PATH变量中有一个npm .
ts-nameof
然后在这个项目中,我需要进行查询以使用顺序进行续订,因此不是硬编码列名或使用安全类型字符串名称(我讨厌字符串!)我发现了ts-nameof .
所以我按照说明操作:
-
npm i ttypescript -D
-
在tsconfig.json中添加插件compilerOptions
-
添加配置"typescript.tsdk"
When things go nuts
-
ttypescript赢了't install because I didn' t有打字稿(呃?)所以让我安装吧:npm i typescript
-
现在让我们使用nameof()方法! Hum Visual Studio Code告诉我它无法找到它 . 所以我补充道
“node_modules / ts-nameof / ts-nameof.d.ts”
在tsconfig.json“files”属性中 . 是的,它有效!
- 不要让我们运行任务"tsc: watch - tsconfig.json" - 是的,它编译!
ReferenceError:未定义nameof
但是运行时不喜欢它......
- 当看终端时它仍然使用tsc而不是tscc,所以从我的理解我必须配置tasks.json所以让我们这样做! Visual Studio确实通过build / watch为我生成了一个基本文件 . 我刚刚添加了命令/类型属性 . (最后参见文件以供参考)
其他用户的奖励:
- 如果看到错误
终端进程以退出代码终止:2
可能是因为你使用的是nodes_modules / npm / ttsc,它只适用于linux / osx(我不知道它可以在windows 10中使用linux嵌入的东西)
- 另外安装ttypescript global可能不是一个好主意,因为它会将它安装在你的用户目录中而你不能在tasks.json中使用%AppData%,所以你必须像C:\ Users \那样使用完整的路径 . 0xcdcdcdcd \应用程序数据\漫游...
Tasks against you
- 让我们通过运行任务"node_modules.bin\ttsc.cmd"编译一次 - 最后!
顺便说一句:我有这个文件的其他问题,请参阅tasks.json的注释底部...
- 现在让我们为每个任务添加标签 - 现在我可以看到两个任务而不是唯一的"node_modules.bin\ttsc.cmd" . 什么,现在正在使用tsc!
Questions/Issues
- 在做这一切的同时,我试图谷歌我的许多问题没有成功 . 我找不到任何东西?好吧也许ttypescript不被任何人使用,但我对tasks.json的所有问题......?
我在使用Visual Studio(而不是代码)使用google搜索typescript / nodejs时遇到了问题,因为每个人都在使用visual studio代码......所以我肯定有些不对劲 .
-
也许使用ts-nameof之外的东西(这导致我使用ttypescript)是一个更好的主意?如果这不是一个好选择,你会推荐我什么 .
-
我仍然需要弄清楚如何配置“监视”任务运行以及如何在tasks.json中有2个任务...
-
继续阅读我遇到的其他问题,例如“typescript.tsdk”或“problemMatchers”选项 .
Files
.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代码似乎与这个文件有关,从文档中我应该能够在“任务”对象中使用命令/类型(我认为“windows”),它不会让我离开 .
此外,当使用此文件运行我的任务时,Visual Studio Code要求我提供problemMatcher并在tasks.json的根目录中添加此属性 .
EDIT: 我也尝试在任何地方填充命令/类型属性(在根目录中,在我的所有任务中,以及"windows"中(在根目录中以及在我的所有任务中) .
.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" ]
}
1 回答
我仍然不知道发生了什么,但我读到的tasks.json syntax似乎是基本的,语言扩展它因此为什么类型可以通过typescript并包含像tsconfig这样的属性 . 不幸的是我没有找到打字稿的语法 .
我最终创建了shell任务,如下所示: