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

Creating a TypeScript project

Some text editors, such as VS Code, give us the ability to deal with TS files as independent units, called File Scope. Although this is very useful for isolated files (as in the following examples), it is recommended that you always create a TypeScript project. You can then modularize your code and use dependency injection between files in the future.

A TypeScript project is created with a file called tsconfig.json, placed at the root of a directory. You will need to indicate to the compiler which files are part of the project, the compile options, and many other settings.

A basic
tsconfig.json file contains the following code:

{ "compilerOptions":
{ "target": "es5",
"module": "commonjs"
}
}


Although the preceding code is very simple and intuitive, we are only indicating which compiler we will use in our project, and also what kind of module. If the code snippet indicates that we are using ECMAScript 5, all TypeScript code will be converted to JavaScript, using ES5 syntax.


Now, let's look at how we can create this file automatically, with the help of the
tsc compiler:

  1. Create a folder, called chapter-02.
  2. Open your Terminal inside the chapter-02 folder.
  3. Type the following command:
tsc --init

We will see the following content, generated by the tsc compiler:

{
"compilerOptions": {
/* Basic Options */
/* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"target": "es5",
/* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"module": "commonjs",
...
/* Strict Type-Checking Options */
/* Enable all strict type-checking options. */
"strict": true,
...
/* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"esModuleInterop": true
/* Source Map Options */
...
/* Experimental Options */
...
}
}

Note that we have omitted some sections. You should see all of the available options; however, most of them are commented. Do not worry about that at this time; later on, we will look at some of the options in more detail.

Now, let's create a TypeScript file, and check that everything goes smoothly.

  1. Open VS Code in the chapter-02 folder and create a new file, called sample-01.ts.
  1. Add the following code to sample-01.ts:
console.log('First Sample With TypeScript');
  1. Go back to your Terminal and type the following command:
tsc sample-01.ts
In VS Code, you can use the integrated Terminal; on the top menu bar, click on View | Integrate Terminal [?`].

Note that another file appears with the same name, but with a .js extension.

If you compare both files, they are exactly the same, because our example is pretty simple, and we are using a simple console.log() function.

As TypeScript is a super set of JavaScript, all of the JS features are available here, too.

主站蜘蛛池模板: 鞍山市| 兴海县| 西盟| 金乡县| 嘉鱼县| 玉环县| 遂溪县| 育儿| 隆昌县| 绵阳市| 房产| 麟游县| 德化县| 阳春市| 扎鲁特旗| 安西县| 昌邑市| 甘肃省| 读书| 鲁山县| 内江市| 亚东县| 广宗县| 砀山县| 深圳市| 县级市| 平塘县| 广元市| 双流县| 广汉市| 大关县| 临夏市| 来宾市| 古丈县| 南康市| 台安县| 斗六市| 文山县| 新宁县| 辽中县| 石渠县|