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
当你在Visual Studio Code (VSCode) 中尝试使用插件如`Code Runner`运行TypeScript (.ts) 文件时,如果遇到乱码问题,可能是由于编码设置不匹配导致的。解决这个问题的步骤通常包括以下几个方面: 1. **检查文件编码**:确保你的 TypeScript 文件是以正确的字符集(例如 UTF-8)保存的。你可以通过文件菜单 -> 选择“编码”选项检查并更改文件编码。 2. **设置终端编码**:打开 VSCode 的用户或工作区配置文件(`.vscode/settings.json`),找到 `terminal.integrated.shellArgs` 或 `terminal.integrated.shell.windows` 配置项,添加 `-encoding` 参数指定终端的默认编码,比如 `"utf-8"`。 ```json "terminal.integrated.shellArgs": ["-enc", "UTF-8"] ``` 3. **配置Code Runner插件**:在 `.vscode\extensions\ms-vscode.codelens-1.6.0\configurations.json` (如果你安装了 Code Lens 扩展)或 `~/.config/coderunner/coderunner.json` (如果没有安装)里,确认你的运行配置也设置了正确的编码。例如: ```json { "type": "shell", "args": ["node", "-e", "your-ts-file"], "options": { "cwd": "${workspaceFolder}", "encoding": "utf-8" } } ``` 4. **重启VSCode**:有时只是简单地关闭再打开编辑器就能解决问题。 5. **检查终端环境变量**:确保系统环境变量 `LANG` 或 `LC_ALL` 设定为支持中文的编码,如 `en_US.UTF-8`。 如果以上步骤都无法解决,试着在终端直接运行命令行(如 Node.js)看是否会出现同样的问题,以便排查是否是外部工具或环境的问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值