Anwarvic
Anwarvic

Reputation: 12992

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. How can I do that knowing that TypeScript is a compiled language? A compiled language means that it compiles the (.ts) file and convert it to Javascript (.js) file first before executing the JavaScript file.

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. 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.

Upvotes: 2

Views: 3133

Answers (2)

Anwarvic
Anwarvic

Reputation: 12992

In this answer, I'm assuming that you already have installed Node.js and typescript on your local machine. 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:

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.

  • Click on Tools tab, then Build System, then New Build System. enter image description here

  • A new file will open.. override its content with the following JSON object. The cmd parameter uses ts-node based on the suggestion of @idleberg. 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.

{
    "shell": true,
    "cmd": ["ts-node", "$file"],
    "selector": "source.js"
}
  • Save the file with the name ts.sublime-build.
  • Finally, build the script using ts as shown below if it wasn't automatically selected.

enter image description here

  • Now, click ctrl + B to run the script. And you should see the output on the sublime text console.

Upvotes: 7

Inx Maurya
Inx Maurya

Reputation: 1155

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.

Upvotes: 2

Related Questions