作者:amosworker
時(shí)間:2018/09/01
Typescript是微軟開發(fā)的一個(gè)JavaScript的超級(jí)誊涯。著名前端框架的Angular和HTML5游戲引擎Egret等都選擇了Typescript作為編寫語言∠鳎“工欲善其事馋劈,必先利其器”锁施,在學(xué)習(xí)Typescript語言之前必須要有一個(gè)良好的開發(fā)環(huán)境。這里筆者選擇了輕量的Visual Studio Code界轩,此工具具有輕量盐须、跨平臺(tái)玩荠、插件豐富等特點(diǎn),是一款非常優(yōu)秀的開發(fā)工具贼邓。
1阶冈、首先需要安裝Nodejs,安裝過程網(wǎng)上很容易搜到立帖,在此不再介紹眼溶。安裝完成后,在命令窗口輸入
$ node -v
v8.11.4
2晓勇、安裝typescript模塊
$ npm install -g typescript
安裝完成后堂飞,可以查看typescript模塊的版本號(hào)
$ tsc -v
Version 3.0.3
3、創(chuàng)建項(xiàng)目目錄绑咱,在命令窗口中進(jìn)入該項(xiàng)目目錄,創(chuàng)建tsconfig.json绰筛。例如項(xiàng)目地址是D:\test\ts。
$ D:
$ cd test\ts
$ tsc --init
在項(xiàng)目目錄下會(huì)生成一個(gè)tsconfig.json文件
{
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
}
里面有很多配置項(xiàng)描融,大家可以根據(jù)各自的需要來定制铝噩。本文用的示例配置如下,
{
"compilerOptions": {
"target": "es5",
"module": "commonjs'",
"outDir": "./js/",
"rootDir": "./tscript/",
"esModuleInterop": true
}
}
新建tscript、js文件夾窿克,分別用作存放typescript骏庸、javascript文件。
4年叮、測(cè)試編譯
在Visual studio code中打開ts目錄具被,在tscript目錄中新建test.ts文件。
class Person {
hobby: string;
constructor(hobby: string) {
this.hobby = hobby;
}
echo() {
return '我就喜歡:' + this.hobby;
}
}
let person = new Person('看書只损,習(xí)武一姿,書法,旅游');
let hobby = person.echo();
console.log(hobby);
然后點(diǎn)擊菜單中的Tasks->Run Task跃惫。
之后會(huì)出現(xiàn)tsc:build 叮叹、tsc:watch兩個(gè)選項(xiàng)
其中tsc:build選項(xiàng)是用于一次編譯。tsc:watch選項(xiàng)可以監(jiān)測(cè)ts文件的改動(dòng)爆存,可以進(jìn)行實(shí)時(shí)編譯蛉顽,非常方便。經(jīng)過編譯后的文件會(huì)存放在之前配置好的js文件目錄下先较。
"use strict";
var Person = /** @class */ (function () {
function Person(hobby) {
this.hobby = hobby;
}
Person.prototype.echo = function () {
return '我就喜歡:' + this.hobby;
};
return Person;
}());
var person = new Person('看書蜂林,習(xí)武遥诉,書法拇泣,旅游');
var hobby = person.echo();
console.log(hobby);
如此就可以進(jìn)行Typescript之旅了噪叙。~~