vscode下搭建Typescript編譯環(huán)境

作者: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之旅了噪叙。~~

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市霉翔,隨后出現(xiàn)的幾起案子睁蕾,更是在濱河造成了極大的恐慌,老刑警劉巖债朵,帶你破解...
    沈念sama閱讀 218,122評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件子眶,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡序芦,警方通過查閱死者的電腦和手機(jī)臭杰,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,070評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谚中,“玉大人渴杆,你說我怎么就攤上這事∠芩” “怎么了磁奖?”我有些...
    開封第一講書人閱讀 164,491評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)某筐。 經(jīng)常有香客問我比搭,道長(zhǎng),這世上最難降的妖魔是什么南誊? 我笑而不...
    開封第一講書人閱讀 58,636評(píng)論 1 293
  • 正文 為了忘掉前任身诺,我火速辦了婚禮,結(jié)果婚禮上抄囚,老公的妹妹穿的比我還像新娘霉赡。我一直安慰自己,他們只是感情好怠苔,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,676評(píng)論 6 392
  • 文/花漫 我一把揭開白布同廉。 她就那樣靜靜地躺著,像睡著了一般柑司。 火紅的嫁衣襯著肌膚如雪迫肖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,541評(píng)論 1 305
  • 那天攒驰,我揣著相機(jī)與錄音蟆湖,去河邊找鬼。 笑死玻粪,一個(gè)胖子當(dāng)著我的面吹牛隅津,可吹牛的內(nèi)容都是我干的诬垂。 我是一名探鬼主播,決...
    沈念sama閱讀 40,292評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼伦仍,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼结窘!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起充蓝,我...
    開封第一講書人閱讀 39,211評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤隧枫,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后谓苟,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體官脓,經(jīng)...
    沈念sama閱讀 45,655評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,846評(píng)論 3 336
  • 正文 我和宋清朗相戀三年涝焙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了卑笨。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,965評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡仑撞,死狀恐怖赤兴,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情派草,我是刑警寧澤搀缠,帶...
    沈念sama閱讀 35,684評(píng)論 5 347
  • 正文 年R本政府宣布,位于F島的核電站近迁,受9級(jí)特大地震影響艺普,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鉴竭,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,295評(píng)論 3 329
  • 文/蒙蒙 一歧譬、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧搏存,春花似錦瑰步、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,894評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至责静,卻和暖如春袁滥,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背灾螃。 一陣腳步聲響...
    開封第一講書人閱讀 33,012評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工题翻, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人腰鬼。 一個(gè)月前我還...
    沈念sama閱讀 48,126評(píng)論 3 370
  • 正文 我出身青樓嵌赠,卻偏偏與公主長(zhǎng)得像塑荒,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子姜挺,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,914評(píng)論 2 355