【发布时间】:2017-07-14 21:04:33
【问题描述】:
我正在学习打字稿并尝试让调试也正常工作。
我正在使用 Visual Studio Code 版本 1.14.1 打字稿版本 2.4.1 NodeJs 版本 8.1.4
这里是代码https://github.com/sherry-ummen/tryingouttypescriptdebugging的存储库
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "tsc",
"isShellCommand": true,
"args": ["-w", "-p", "."],
"showOutput": "silent",
"isBackground": true,
"problemMatcher": "$tsc-watch"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "node",
"request": "launch",
"program": "${workspaceRoot}/Test.ts",
"stopOnEntry": false,
"args": [],
"cwd": "${workspaceRoot}",
"preLaunchTask": null,
"runtimeExecutable": null,
"runtimeArgs": [
"--nolazy"
],
"env": {
"NODE_ENV": "development"
},
"console": "integratedTerminal",
"sourceMaps": true,
"outFiles": ["${workspaceRoot}/out/*.js"]
},
{
"name": "Attach",
"type": "node",
"request": "attach",
"port": 5858
}
]
}
tsconfig.json
{
"compilerOptions": {
"target": "es6",
"outDir": "out/",
"sourceMap": true
},
"files": [
"IShape.ts",
"Circle.ts",
"Triangle.ts",
"Test.ts"
]
}
所以编译工作正常,至少不会出错。但是当我在 Visual Studio 代码上按 F5 运行它时,我得到以下错误
有人可以指导我如何使用 Visual Studio 代码来完成这项工作吗?
【问题讨论】:
-
您为什么希望 Triangle、Circle 和 IShape 出现在您的 Drawing 命名空间中?
<reference />s 也仅适用于类型,不适用于实际值。为此,您需要导入或要求它们。 -
好的,我仍在学习和尝试打字稿,所以我的知识非常有限,但我认为我在 Drawing 命名空间中定义了 IShape 和其他内容,因此我应该可以在我的 Test.ts 中访问它。
-
你没有。 Triangle、Circle 等位于它们自己的文件中,因此它们没有在您当前工作的命名空间中定义。您需要先导入它们,然后按它们的专有名称使用它们,即
Triangle、Circle等 -
关于您的参考建议,您能详细说明一下您的意思吗?
-
要在文件中使用这些对象,您需要
import Triangle from './Triangle',而不是<reference />。
标签: node.js typescript visual-studio-code