# VSCode配置Code Runner + Deno直接运行ts代码

最近LeetCode的一些题目开始支持ts了,所以我就开始用ts做一些题目。事实上现在vscode对ts的支持已经相当不错了,包括语法高亮啥的。但是无论是什么编辑器(包括我最爱的WebStorm),都不能直接运行ts代码,这给本地调试带来了很大的不便。

应该很多人知道vscode有个叫code runner的插件,可以很方便地去运行代码,然后他给的办法是去装一个ts-node的npm包,但我觉得没啥意思。正好一直听说deno的1.0版本已经发布了,能够直接运行ts代码,就索性装个deno,然后配置一下code runner的执行命令,一步到位。

deno官网的安装说明已经很详细了。比如我用的Windows(powershell):

iwr https://deno.land/x/install/install.ps1 -useb | iex

别的操作系统同理,看一下官方文档就OK。

装完之后,在vscode里设置一下code runner的Executor Map属性。在settings.json里加上这么一段:

"code-runner.executorMap": {
    "typescript": "deno run $fullFileName"
}

然后就可以了。

可能有人对这个$fullFileName变量有疑问,这个其实是code runner插件的预置变量。按照作者的说法,有这么几个变量,可以在配置里使用,会被自动替换。这几个变量顾名思义:

  • Supported customized parameters

    • $workspaceRoot: The path of the folder opened in VS Code

    • $dir: The directory of the code file being run

    • $dirWithoutTrailingSlash: The directory of the code file being run without a trailing slash

    • $fullFileName: The full name of the code file being run

    • $fileName: The base name of the code file being run, that is the file without the directory

    • $fileNameWithoutExt: The base name of the code file being run without its extension

    • $driveLetter: The drive letter of the code file being run (Windows only)

    • $pythonPath: The path of Python interpreter (set by Python: Select Interpreter command)

Please take care of the back slash and the space in file path of the executor

  • Back slash: please use \\
  • If there ares spaces in file path, please use \" to surround your file path
最后更新于: 6/25/2020, 2:10:06 PM