• 八方資訊網歡迎您!
    八方資訊網>時尚>正文

    Typescript tsconfig.json 詳解

    2020-03-29 09:16:49 來源: 閱讀:

    環境搭建

    安裝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 'rollup-plugin-json';
    import typescript from 'rollup-plugin-typescript';

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

    package.json

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

    main.ts

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

    const user = "Jane User,this is js hello from ts";

    document.body.textContent = greeter(user);

    index.html

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

    </head>
    <body>

    <script src="./app.js"></script>

    </body>
    </html>
    • 打開index.html, 界面出現Hello, Jane User,this is js hello from ts ,恭喜你,你已經typescript入門了!??!

    typescript配置文件

    typescript支持兩種配置文件:

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

    http://json.schemastore.org/tsconfig

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

    其中,

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

    移除代碼中注釋

    {
    "files": [
    "src/main.ts"
    ],
    "compilerOptions": {
    "removeComments": 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('xiaoming','hello');

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

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

    開啟null、undefined檢測

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

    參考文獻

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

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

    聯系郵箱:simple2012hcz@126.com

    本文為企業推廣,本網站不做任何建議,僅提供參考,作為信息展示!

    推薦閱讀:專業相機怎么調

    網友評論
    請登錄后進行評論| 0條評論

    請文明發言,還可以輸入140

    您的評論已經發表成功,請等候審核

    小提示:您要為您發表的言論后果負責,請各位遵守法紀注意語言文明

    回到首頁 回到頂部
    八方資訊網 關于我們| 聯系我們| 招聘信息| 老版地圖| 網站地圖
    免責聲明:八方資訊網所有文字、圖片、視頻、音頻等資料均來自互聯網,不代表本站贊同其觀點,本站亦不為其版權負責。相關作品的原創性、文中陳述文字以及內容數據龐雜本站無法一一核實,如果您發現本網站上有侵犯您的合法權益的內容,請聯系我們,本網站將立即予以刪除!
    Copyright © 2012-2019 http://m.quan28.cn, All rights reserved.
    主站蜘蛛池模板: 狠狠精品干练久久久无码中文字幕 | 久久精品一区二区三区中文字幕 | 久久亚洲欧美国产精品| 无码国产精品一区二区免费模式| 久久精品亚洲欧美日韩久久| 国产精品夜色视频一级区| 精品久久久久久国产潘金莲 | 国产精品免费一区二区三区| 国产A级毛片久久久精品毛片| 国产精品99无码一区二区| 久久精品国产精品国产精品污| 无码精品人妻一区二区三区AV| 久久精品国产影库免费看| 99久久er这里只有精品18| 久久91精品国产91久久小草| 欧美肥屁VIDEOSSEX精品| 国产成人亚洲精品影院| 亚洲精品乱码久久久久久中文字幕| 无码人妻精品一区二区三 | 亚洲欧美日韩精品| 精品人妻人人做人人爽| 99精品国产自在现线观看| 香蕉久久夜色精品升级完成| 国产精品va在线观看无码| HEYZO无码综合国产精品227| 免费视频成人国产精品网站| 欧美精品天天操| 久久精品嫩草影院| 国产精品一区二区不卡| 精品国产三级a∨在线欧美| 国产日韩精品欧美一区| 亚洲AV无码国产精品麻豆天美 | 99精品国产福利在线观看| 无码aⅴ精品一区二区三区浪潮| 久久国产精品视频| 亚洲精品国产精品国自产观看| 精品久久久久久无码国产| 日本人精品video黑人| 白浆都出来了视频国产精品| 精品综合久久久久久88小说| 久久精品天天中文字幕人妻 |