tsconfig - typescript配置
// tsconfig.json{"compilerOptions": {"moduleResolution": "Node", // 模块解析规则"target": "es5", // 指定ECMAScript目标版本"jsx": "preserve","module":"es2015", // 模块系统代码"lib": ["es2015", "es2016", "es2017", "dom"], // 指定api使用的版本,例如Map或Math"declaration": true, // 生成相应的 .d.ts文件"outDir": "dist/lib", // .js的文件输出路径"declarationDir": "dist/types", // .d.ts文件输出路径"strict": true // 严格类型检查,"allowSyntheticDefaultImports": true // 可使用默认导入语法 import React from "react"},"include": ["src"]}
:::tips strict开启后会自动启用以下规则
- strictNullChecks
- strictBindCallApply
- strictPropertyInitialization
- strictFunctionTypes
- noImplicitThis(强制显示注解函数中this的类型,不强制类或对象的函数注解this) :::
:::tips module详解
- 如果使用systemJS模块加载器,把module设为systemjs
- 如果使用兼容ES2015的模块打包工具,例如webpack或rollup,设为es2015或更高版本
- 如果是发布一个库,应该使用umd
- 如果使用兼容ES2015的模块打包工具,而且代码中用到了动态导入,设为esnext
- 如果使用commonJS打包工具打包模块,例如Browserify,设为commonjs
- 如果打算使用RequireJS或其他AMD模块加载器加载代码,把module设为umd :::
RollupJs - 模块化构建
// rollup.config.tsimport typescript from 'rollup-plugin-typescript2'import pkg from './package.json'import { terser } from "rollup-plugin-terser"export default {input: 'src/index.ts',plugins: [typescript({ useTsconfigDeclarationDir: true }) // to find tsconfig.json],output: [{file: pkg.main,format: 'umd', // cmj or iifename: 'easyFetch', // bundle nameplugins: [terser()] // minify bundle},{ file: pkg.umd, format: "umd", name: 'easyFetch' },{ file: pkg.module, format: 'es' },]};
esLint - 代码风格
// .eslintrc.jsmodule.exports = {'env': {'browser': true,'es2021': true,'node': true},'extends': ['eslint:recommended','plugin:@typescript-eslint/recommended'],'parser': '@typescript-eslint/parser','parserOptions': {'ecmaVersion': 12,'sourceType': 'module'},'plugins': ['@typescript-eslint'],'rules': {'indent': [ // 缩进使用tab'error','tab'],'linebreak-style': [ // 换行风格'error','unix'],'quotes': [ // 单引号'error','single'],'semi': [ // 不使用分号'error','never'],"@typescript-eslint/no-explicit-any": 0, // 禁用any提示"@typescript-eslint/explicit-module-boundary-types": 0,"@typescript-eslint/no-extra-semi": 0}}
prettier - 格式化代码
// .prettierrc.jsmodule.exports = {// 箭头函数只有一个参数的时候可以忽略括号arrowParens: 'avoid',tabWidth: 2,useTabs: true,parser: 'typescript',// 行宽printWidth: 120,// 分号semi: false,// 使用单引号singleQuote: true,// 后置逗号,多行对象、数组在最后一行不增加逗号trailingComma: 'none'}
