TypeScript 相关配置

66 阅读1分钟

image.png

1、安装相关配置

npm instatll typescript @types/react @types/react-dom

2、编辑tsconfig.json文件

{
  "compilerOptions": {
    "target": "es5",                          /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
    "module": "es2015",                     /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
    "allowJs": true,                       /* Allow javascript files to be compiled. */
    "jsx": "react",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    "outDir": "./",                        /* Redirect output structure to the directory. */
    "rootDir": "./",                       /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    "strict": true,                           /* Enable all strict type-checking options. */
    "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    "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": {
      "pages/*": [
        "src/pages/*"
      ],
      "common/*": [
        "src/common/*"
      ],
      "@utils/*": [
        "src/utils/*"
      ],
      "@components/*": [
        "src/components/*"
      ],
      "context/*": [
        "src/context/*"
      ],
      "api/*": [
        "src/api/*"
      ],
      "store/*": [
        "src/store/*"
      ],
    },                           /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
  "esModuleInterop": true,                   /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
    "experimentalDecorators": true,        /* Enables experimental support for ES7 decorators. */
  },
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules",
    "config",
    "dist"
  ]
}

3、 使用eslint还需要安装进行转换

npm install @typescript-eslint/eslint-plugin @typescript-eslint/parser