官术网_书友最值得收藏!

Installing the prerequisites

The contents of this chapter are expected to work on all major and up-to-date desktop operating systems, including Windows, OS X, and Linux.

As Node.js is widely used as a runtime for server applications as well as frontend build tools, we are going to make it the main playground of code in this book.

TypeScript compiler, on the other hand, is the tool that compiles TypeScript source files into plain JavaScript. It's available on multiple platforms and runtimes, and in this book we'll be using the Node.js version.

Installing Node.js

Installing Node.js should be easy enough. But there's something we could do to minimize incompatibility over time and across different environments:

  • Version: We'll be using Node.js 6 with npm 3 built-in in this book. (The major version of Node.js may increase rapidly over time, but we can expect minimum breaking changes directly related to our contents. Feel free to try a newer version if it's available.)
  • Path: If you are installing Node.js without a package manager, make sure the environment variable PATH is properly configured.

Open a console (a command prompt or terminal, depending on your operating system) and make sure Node.js as well as the built-in package manager npm is working:

$ node -v 6.x.x $ npm -v 3.x.x 

Installing TypeScript compiler

TypeScript compiler for Node.js is published as an npm package with command line interface. To install the compiler, we can simply use the npm install command:

$ npm install typescript -g 

Option -g means a global installation, so that tsc will be available as a command. Now let's make sure the compiler works:

$ tsc -v 
Version 2.x.x

Note

You may get a rough list of the options your TypeScript compiler provides with switch -h. Taking a look into these options may help you discover some useful features.

Before choosing an editor, let's print out the legendary phrase:

  1. Save the following code to file test.ts:
    function hello(name: string): void { 
            console.log(`hello, ${name}!`); 
          } 
           
          hello('world'); 
    
  2. Change the working directory of your console to the folder containing the created file, and compile it with tsc:
     $ tsc test.ts
    
  3. With luck, you should have the compiled JavaScript file as test.js. Execute it with Node.js to get the ceremony done:
    $ node test.js
    hello, world!
    

Here we go, on the road to retire your CTO.

主站蜘蛛池模板: 木里| 攀枝花市| 漯河市| 翁源县| 揭阳市| 河池市| 辰溪县| 红原县| 化州市| 克什克腾旗| 甘洛县| 容城县| 都安| 乐平市| 万盛区| 锦州市| 雅江县| 枣阳市| 个旧市| 马山县| 利川市| 依兰县| 乌拉特后旗| 洛扎县| 三明市| 花垣县| 化州市| 清徐县| 莱芜市| 长宁区| 石楼县| 上虞市| 永登县| 阜宁县| 万源市| 扎兰屯市| 凤凰县| 资阳市| 元阳县| 盘锦市| 兴城市|