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

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.

主站蜘蛛池模板: 叙永县| 赤峰市| 株洲县| 新安县| 西盟| 鄂伦春自治旗| 富阳市| 石屏县| 鹿泉市| 当涂县| 汕头市| 和硕县| 台北县| 沙雅县| 吴忠市| 霍邱县| 龙岩市| 溧阳市| 孟津县| 永修县| 海盐县| 根河市| 达拉特旗| 静乐县| 玛纳斯县| 格尔木市| 白水县| 益阳市| 大丰市| 富川| 芮城县| 罗江县| 静安区| 于田县| 江陵县| 时尚| 浦北县| 大名县| 星子县| 同德县| 句容市|