简体   繁体   English

在 Sublime 文本上运行 TypeScript 文件

[英]Run TypeScript file on Sublime text

I want to build a TypeScript file (with extension.ts) in the sublime text 3 console and show the output in the console the same way sublime does with python .我想在 sublime text 3 控制台中构建一个 TypeScript 文件(带有扩展名.ts),并在控制台中显示 output,就像 sublime 对python . How can I do that knowing that TypeScript is a compiled language?知道 TypeScript 是一种编译语言,我该怎么做? A compiled language means that it compiles the (.ts) file and convert it to Javascript (.js) file first before executing the JavaScript file.编译语言意味着它首先编译 (.ts) 文件并将其转换为 Javascript (.js) 文件,然后再执行 JavaScript 文件。

I have tried installing the TypeScript plugin from the sublime package installer, but it needs a certain architecture to run the file as explained here .我已经尝试从 sublime package 安装程序安装 TypeScript 插件,但它需要一定的体系结构来运行文件,如此所述。 Also this question wasn't helpful as it shows how to build after saving and it didn't say anything about running a standalone file. 这个问题也没有帮助,因为它显示了保存后如何构建,并且没有说明运行独立文件的任何内容。

In this answer, I'm assuming that you already have installed Node.js and typescript on your local machine.在这个答案中,我假设您已经在本地计算机上安装了 Node.js 和 typescript 。 If not, you can install Node.js from here and after that, you can run the following command to install typescript and ts-node using node package manager:如果没有,您可以从这里安装 Node.js,之后,您可以运行以下命令来安装typescriptts-node使用节点 package 管理器:

npm install -g typescript
npm install -g ts-node

Now, you are ready to follow these steps:现在,您已准备好执行以下步骤:

  • Open the typescript file -that you need to run- on sublime text 3.打开 typescript 文件 - 您需要在 sublime text 3 上运行该文件。
  • Click on Tools tab, then Build System , then New Build System .单击Tools选项卡,然后单击Build System ,然后单击New Build System 在此处输入图像描述

  • A new file will open.. override its content with the following JSON object.将打开一个新文件.. 使用以下 JSON object 覆盖其内容。 The cmd parameter uses ts-node based on the suggestion of @idleberg. cmd参数根据@idleberg 的建议使用ts-node And @OdatNurd suggested adding "selector": "source.ts" to make the build automatically selected for TypeScript files so you don't have to manually select it. @OdatNurd 建议添加 "selector": "source.ts" 以使构建自动选择 TypeScript 文件,因此您不必手动 select 它。

{
    "shell": true,
    "cmd": ["ts-node $file"],
    "selector": "source.js"
}
  • Save the file with the name ts.sublime-build .使用名称ts.sublime-build保存文件。
  • Finally, build the script using ts as shown below if it wasn't automatically selected.最后,如果没有自动选择,请使用ts构建脚本,如下所示。

在此处输入图像描述

  • Now, click ctrl + B to run the script.现在,单击ctrl + B运行脚本。 And you should see the output on the sublime text console.您应该在 sublime 文本控制台上看到 output。

Open package control -> type or select " Install Package " -> type or select " TypeScript " -> press enter Open package control -> type or select " Install Package " -> type or select " TypeScript " -> press enter

Ex -> for mac os: command+shift+p to open package control after that type or choose " Install Package ", it will give list of packages to choose, type " TypeScript " and press enter. Ex -> 对于 mac os: command+shift+p在该类型之后打开 package 控件或选择“安装 Package ”,它将给出要选择的软件包列表,输入“ Z558B544CF685F39D34E4903E39 ”并按 Enter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM