Typescript的简单配置(二)

153 阅读2分钟

执行指令

tsc 直接根据配置文件进行编译 tsc ***.ts 手动编译成js tsc ***.ts -w 检测文件改变自动编译成js

配置文件

根目录下创建 tsconfig.js

{
    /*
        tsconfig.json 是ts编译器的配置文件, ts编译器可以根据它的信息来对代码进行编译
        "include" 用来指定那些ts文件需要被编译
    */
    "include": [
        // **表示任意目录,*表示任意文件, 这里表示src下任意ts都会被编译
        "./src/**/*"
    ],
    
    
    // 排除文件
    "exclude": [
        "./src/hello/**/*"
    ],

    /*
        compilerOptions 编译器选项
    */
    "compilerOptions": {
        // target 用来指定ts被编译的ES版本
        // 'es3', 'es5', 'es6', 'es2015', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'esnext'.
        "target": "es2015",


        // 指定要使用模块化的规范
        // 'none', 'commonjs', 'amd', 'system', 'umd', 'es6', 'es2015', 'es2020', 'es2022', 'esnext', 'node12', 'nodenext'.
        "module": "amd",

        // lib用来指定项目要使用的库
        // 'es5', 'es6', 'es2015', 'es7', 'es2016', 'es2017', 'es2018', 'es2019', 'es2020', 'es2021', 'esnext', 'dom', 'dom.iterable', 'webworker', 'webworker.importscripts', 'webworker.iterable', 'scripthost', 'es2015.core', 'es2015.collection', 'es2015.generator', 'es2015.iterable', 'es2015.promise', 'es2015.proxy', 'es2015.reflect', 'es2015.symbol', 'es2015.symbol.wellknown', 'es2016.array.include', 'es2017.object', 'es2017.sharedmemory', 'es2017.string', 'es2017.intl', 'es2017.typedarrays', 'es2018.asyncgenerator', 'es2018.asynciterable', 'es2018.intl', 'es2018.promise', 'es2018.regexp', 'es2019.array', 'es2019.object', 'es2019.string', 'es2019.symbol', 'es2020.bigint', 'es2020.promise', 'es2020.sharedmemory', 'es2020.string', 'es2020.symbol.wellknown', 'es2020.intl', 'es2021.promise', 'es2021.string',
        // 'es2021.weakref', 'es2021.intl', 'esnext.array', 'esnext.symbol', 'esnext.asynciterable', 'esnext.intl', 'esnext.bigint', 'esnext.string', 'esnext.promise', 'esnext.weakref'.
        // 浏览器下 默认情况不用动
        // "lib": []

        // outDir 用来指定编译后文件所在的目录
        "outDir": "./dist",

        // 将代码合并成一个文件
        // 设置了outFile后, 所有的全局作用域中的代码会合并到同一个文件
        // "outFile": "./dist/app.js",

        // 是否对js文件进行处理,默认是false
        "allowJs": true,

        // 是否对js语法进行检查,默认是false
        "checkJs": false,

        // 是否移除注释,默认是false
        "removeComments": false,

        // 不生产编译文件,只检查
        "noEmit": false,

        // 当有错误时不生成编译后的文件
        "noEmitOnError": false,


        // 所有严格检查的总开关,这个打开,下面的都打开,开发建议设置为true
        "strict": true,

        // 编译后的js中使用严格模式
        "alwaysStrict": false,

        // 不允许隐私any
        "noImplicitAny": true,

        // 不允许不明确类型的this
        "noImplicitThis": true,

        // 严格检查空值
        "strictNullChecks": true
    }
}