If you already have a subscription, you can sign in.
Enjoy free content straight from your inbox 💌
00:00
Here we have a basic TypeScript source code with a single type annotation. Now, TypeScript is not something that can be run natively on node, so if you try to run it through it, you will actually get a syntax error as shown over here. Of course, the simplest way to run any piece of TypeScript code is to first compile it to JavaScript, and we can do that by running N-P-X-T-A-C. And now once we have the output JavaScript in the lib folder, we can simply execute it through Node. Now if you wanna skip this compile type script to JavaScript and then execute it process,
00:33
there is an NPM package called ts node that can compile your type script to JavaScript on the fly and then execute it through Node in a single step. So in order to run this TypeScript source code, we simply run NPX TS node source index ts. This will bring in the TS node package from the internet and then execute it locally. And you can see that the output matches what we expected. Now with any NPM package, if you wanna skip this download from the internet flow, you can install that package into your folder. So for TS node we simply run NPMI TS node,
01:07
and once the installation is complete, if you run the same command again, you can see that it executes much faster. Now if you find yourself running some NPM command again and again, you can even move it into the package of Jason script section to give it a nicer name. So within our script section, we create an entry for start, which will execute TS node for source index for Ts. And now we can simply open up the terminal and run NPM start, which will in turn run DS node and you can see that the output matches what we expected.