国产午夜男女在线|欧美日本一道高清国产|亚洲日韩乱码中文字幕|麻豆国产97在线精品一区|日韩一区2区三区另类图片|亚洲精品国产99在线观看|亚洲国产午夜福利精品大秀在线|一级做a爰片性色毛片免费网站

          1. <form id="n2a4a"><nav id="n2a4a"></nav></form>
          2. 您當(dāng)前的位置 :寧夏資訊網(wǎng) > 微商 >  內(nèi)容正文
            投稿

            Typescript tsconfig.json 詳解

            寧夏資訊網(wǎng) 2020-03-29 10:31:28 來源: 閱讀:-

            環(huán)境搭建

            安裝ts

            npm i -g typescript

            初始化工程

            mkdir ts-demo
            npm init -y

            安裝rollup

            npm i -g rollup
            npm i rollup -D

            添加rollup.config.js

            touch rollup.config.js 
            npm i rollup-plugin-json -D
            npm i rollup-plugin-typescript typescript tslib -D

            import json from &#39;rollup-plugin-json&#39;;
            import typescript from &#39;rollup-plugin-typescript&#39;;

            export default {
            input: &#39;src/main.ts&#39;,
            output: {
            file: &#39;dist/app.js&#39;,
            format: &#39;cjs&#39;
            },
            plugins: [ typescript() ]
            };

            package.json

            {
            &#34;name&#34;: &#34;ts-demo&#34;,
            &#34;version&#34;: &#34;1.0.0&#34;,
            &#34;description&#34;: &#34;&#34;,
            &#34;main&#34;: &#34;index.js&#34;,
            &#34;scripts&#34;: {
            &#34;test&#34;: &#34;echo \&#34;Error: no test specified\&#34; &amp;&amp; exit 1&#34;,
            &#34;dev-build&#34;: &#34;rollup -c&#34;,
            &#34;dev&#34;: &#34;npm run dev-build &amp;&amp; node ./dist/app.js&#34;
            },
            &#34;keywords&#34;: [],
            &#34;author&#34;: &#34;&#34;,
            &#34;license&#34;: &#34;ISC&#34;,
            &#34;devDependencies&#34;: {
            &#34;rollup&#34;: &#34;^1.27.5&#34;,
            &#34;rollup-plugin-json&#34;: &#34;^4.0.0&#34;,
            &#34;rollup-plugin-typescript&#34;: &#34;^1.0.1&#34;,
            &#34;tslib&#34;: &#34;^1.10.0&#34;,
            &#34;typescript&#34;: &#34;^3.7.2&#34;
            }
            }

            main.ts

            // src/main.ts
            function greeter(person: string):string {
            return &#34;Hello, &#34; + person;
            }

            const user = &#34;Jane User,this is js hello from ts&#34;;

            document.body.textContent = greeter(user);

            index.html

            &lt;!DOCTYPE html&gt;
            &lt;html lang=&#34;en&#34;&gt;
            &lt;head&gt;
            &lt;meta charset=&#34;UTF-8&#34;&gt;
            &lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0&#34;&gt;
            &lt;meta http-equiv=&#34;X-UA-Compatible&#34; content=&#34;ie=edge&#34;&gt;
            &lt;title&gt;Document&lt;/title&gt;

            &lt;/head&gt;
            &lt;body&gt;

            &lt;script src=&#34;./app.js&#34;&gt;&lt;/script&gt;

            &lt;/body&gt;
            &lt;/html&gt;
            • 打開index.html, 界面出現(xiàn)Hello, Jane User,this is js hello from ts ,恭喜你,你已經(jīng)typescript入門了?。?!

            typescript配置文件

            typescript支持兩種配置文件:

            • tsconfig.json
            • xxx.json,其中包含其需要的配置項
              下方將側(cè)重介紹tsconfig.json

            http://json.schemastore.org/tsconfig

            {
            &#34;files&#34;: [ # 指定需要編譯文件,相對配置文件所在
            &#34;core.ts&#34;,
            &#34;sys.ts&#34;,
            &#34;types.ts&#34;,
            &#34;scanner.ts&#34;,
            &#34;parser.ts&#34;,
            &#34;utilities.ts&#34;,
            &#34;binder.ts&#34;,
            &#34;checker.ts&#34;,
            &#34;emitter.ts&#34;,
            &#34;program.ts&#34;,
            &#34;commandLineParser.ts&#34;,
            &#34;tsc.ts&#34;,
            &#34;diagnosticInformationMap.generated.ts&#34;
            ],
            &#34;exclude&#34;: [ # 指定不需要編譯文件
            &#34;node_modules&#34;,
            &#34;**/*.spec.ts&#34;
            ],
            &#34;include&#34;: [ # 指定需要編譯文件; 不配置files,include,默認(rèn)除了exclude的所有.ts,.d.ts,.tsx
            &#34;src/**/*&#34;
            ],
            # 指定基礎(chǔ)配置文件路徑 大部分配置 compilerOptions, files, include, and exclude。切忌循環(huán)引用。
            &#34;extends&#34;: &#34;./configs/base&#34;,
            &#34;compilerOptions&#34;: { # 告知TypeScript 編譯器怎么編譯
            &#34;baseUrl&#34;: &#34;./&#34;,
            &#34;paths&#34;: { # 相對于baseUrl配置
            &#34;jquery&#34;: [&#34;node_modules/jquery/dist/jquery&#34;] ,
            &#34;*&#34;: [
            &#34;*&#34;,
            &#34;generated/*&#34;
            ]
            },
            &#34;rootDirs&#34;:[ # 找平路徑配置依賴
            &#34;src/views&#34;,
            &#34;generated/templates/views&#34;
            ],
            &#34;module&#34;: &#34;commonjs&#34;,
            &#34;noImplicitAny&#34;: true,
            &#34;removeComments&#34;: true, # 移除代碼注解
            &#34;preserveConstEnums&#34;: true,
            &#34;sourceMap&#34;: true
            &#34;types&#34;: [] #不會自動導(dǎo)入@types定義的包
            &#34;noResolve&#34;:true , # 不會自動導(dǎo)入import 依賴, 編譯會報錯
            &#34;downlevelIteration&#34;:true # 進(jìn)行js 語法降級 for..of
            &#34;module&#34;: &#34;esnext&#34;,
            &#34;moduleResolution&#34;: &#34;node&#34;,
            &#34;strictNullChecks&#34;: true # 開啟null,檢測
            &#34;target&#34;:&#39;ES5&#39;
            &#34;strictBindCallApply&#34;:true
            &#34;skipLibCheck&#34;:true,
            },
            # 以上屬性,為常用配置屬性
            &#34;compileOnSave&#34;: false, # 整個工程而言,需要編譯器支持,譬如Visual Studio 2015 with TypeScript 1.8.4+
            &#34;typeAcquisition&#34;:{ # 整個工程的類型定義.d.ts
            &#34;enable&#34;:false, # 默認(rèn)值 false
            &#34;include&#34; : [&#34;jquery&#34;, &#34;lodash&#34;]
            &#34;exclue&#34;:[&#34;jquery&#34;, &#34;lodash&#34; ]
            },
            &#34;references&#34;:[{ # 引用的工程
            path: &#39;xxxx&#39;
            }]
            }

            其中,

            • 相對路徑,都是相對配置文件所在路徑
            • 優(yōu)先級:命令行配置 &gt; files &gt; exclude &gt; include
            • exclude默認(rèn)為:nodemodules, bowercomponents, jspm_packages and outDir配置項
            • A.ts 引用B.ts ; A.ts在files 或者include中,B.ts也會被默認(rèn)導(dǎo)入。
            • 一個路徑或者一個文件,在include與exclude之間是互斥的。
            • typeRoots與@types互斥,types、@types也可以互斥。

            移除代碼中注釋

            {
            &#34;files&#34;: [
            &#34;src/main.ts&#34;
            ],
            &#34;compilerOptions&#34;: {
            &#34;removeComments&#34;: true,
            }
            }
            // main.ts
            //add the person type
            interface Person{
            firstName: string;
            lastName: string;
            }
            class Student{
            firstName: string;
            lastName: string;
            constructor(firstName:string , lastName: string){
            this.firstName = firstName;
            this.lastName = lastName;
            }
            }
            // add the greeter
            function greeter(person: Person):string {
            return `Hello,${person.firstName}:${person.lastName}`
            }
            //new a student
            const user = new Student(&#39;xiaoming&#39;,&#39;hello&#39;);

            document.body.textContent = greeter(user);
            //編譯后
            &#39;use strict&#39;;

            var Student = (function () {
            function Student(firstName, lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
            }
            return Student;
            }());
            function greeter(person) {
            return &#34;Hello,&#34; + person.firstName + &#34;:&#34; + person.lastName;
            }
            var user = new Student(&#39;xiaoming&#39;, &#39;hello&#39;);
            document.body.textContent = greeter(user);

            開啟null、undefined檢測

            {
            &#34;files&#34;: [&#34;./src/main.ts&#34;],
            &#34;compilerOptions&#34;: {
            &#34;removeComments&#34;: true,
            &#34;declaration&#34;: true,
            &#34;declarationDir&#34;: &#34;./&#34;,
            &#34;noResolve&#34;: false,
            &#34;strictNullChecks&#34;: true
            },
            }
            const user ;
            user = new Student(&#39;xiaoming&#39;,&#39;hello&#39;); # 編譯報錯

            參考文獻(xiàn)

            • www.rollupjs.com/
            • www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html
            • github.com/rollup/rollup-plugin-typescript
            • json.schemastore.org/tsconfig

            本文作者:前端首席體驗師(CheongHu)

            聯(lián)系郵箱:simple2012hcz@126.com

            (正文已結(jié)束)

            推薦閱讀:icloud查找我的iphone

            免責(zé)聲明及提醒:此文內(nèi)容為本網(wǎng)所轉(zhuǎn)載企業(yè)宣傳資訊,該相關(guān)信息僅為宣傳及傳遞更多信息之目的,不代表本網(wǎng)站觀點(diǎn),文章真實(shí)性請瀏覽者慎重核實(shí)!任何投資加盟均有風(fēng)險,提醒廣大民眾投資需謹(jǐn)慎!

            網(wǎng)站簡介 - 聯(lián)系我們 - 營銷服務(wù) - 老版地圖 - 版權(quán)聲明 - 網(wǎng)站地圖
            Copyright.2002-2019 寧夏資訊網(wǎng) 版權(quán)所有 本網(wǎng)拒絕一切非法行為 歡迎監(jiān)督舉報 如有錯誤信息 歡迎糾正
            浑源县| 老河口市| 水富县| 乌兰县| 苍梧县| 盱眙县| 牡丹江市| 松阳县| 府谷县| 象州县| 大宁县| 左权县| 原平市| 澄江县| 南靖县| 渑池县| 陇川县| 修文县| 黑龙江省| 海口市| 淳化县| 临汾市| 涟水县| 常宁市| 明光市| 理塘县| 凤凰县| 六盘水市| 阿拉善右旗| 个旧市| 垦利县| 甘肃省| 盱眙县| 微山县| 岳池县| 五家渠市| 安义县| 个旧市| 澳门| 盈江县| 泰来县|