JS和TS共存方案

webpack配置

增加ts-loader

  {
                    test: /\.(ts|tsx)$/,
                    include,
                    exclude: /node_modules/,
                    use: ['ts-loader']
  }

ESLINT配置

注意使用overrides属性,可以在overrides属性里配置对应规则

{
  "root": true,
  "env": {
    "serviceworker": true,
    "browser": true,
    "es6": true,
    "jest/globals": true
  },
  "globals": {
    "Atomics": "readonly",
    "SharedArrayBuffer": "readonly"
  },
  "overrides": [
    {
      "files": [
        "./src/**/*.js?(x)"
      ],
      "parser": "@babel/eslint-parser",
      "plugins": [
        "jsx-a11y",
        "react",
        "react-hooks",
        "prettier",
        "jest"
      ],
      "extends": [
        "eslint:recommended",
        "plugin:prettier/recommended",
        "plugin:jest/recommended"
      ],
      "rules": {
        "no-unused-vars": 0,
        "@typescript-eslint/explicit-function-return-type": 0,
        "@typescript-eslint/explicit-module-boundary-types": 0,
        "prettier/prettier": 1,
        // 使用prettier作为eslint规则
        "react-hooks/rules-of-hooks": "error",
        "react-hooks/exhaustive-deps": "warn",
        // 以上两行是精华,必须添加,使用钩子函数时才会提示警告/错误
        "react/jsx-filename-extension": [
          1,
          {
            "extensions": [
              ".js",
              ".jsx"
            ]
          }
        ],
        "linebreak-style": 0,
        "no-plusplus": [
          1,
          {
            "allowForLoopAfterthoughts": true
          }
        ],
        "react/react-in-jsx-scope": 0,
        "react/jsx-props-no-spreading": 0,
        "import/prefer-default-export": 0,
        "no-unused-expressions": [
          "error",
          {
            "allowShortCircuit": true,
            "allowTernary": true
          }
        ],
        "no-restricted-syntax": 0,
        "no-use-before-define": [
          "error",
          {
            "functions": false
          }
        ],
        "jsx-a11y/no-static-element-interactions": 0,
        "jsx-a11y/interactive-supports-focus": [
          "error",
          {
            "tabbable": [
              "button",
              "checkbox",
              "link",
              "searchbox",
              "spinbutton",
              "switch",
              "textbox"
            ]
          }
        ],
        "react/destructuring-assignment": 0,
        "jsx-a11y/click-events-have-key-events": 0,
        "jest/no-disabled-tests": "warn",
        "jest/no-focused-tests": "error",
        "jest/no-identical-title": "error",
        "jest/prefer-to-have-length": "warn",
        "jest/valid-expect": "error",
        "class-methods-use-this": 0,
        "no-param-reassign": 0,
        "no-console": [
          "error",
          {
            "allow": [
              "warn",
              "error"
            ]
          }
        ],
        "default-param-last": 0
      }
    },
    {
      "files": [
        "./src/**/*.ts?(x)"
      ],
      "parser": "@typescript-eslint/parser",
      "plugins": [
        "@typescript-eslint",
        "react-hooks"
      ],
      "extends": [
        "plugin:react/recommended",
        "plugin:@typescript-eslint/recommended",
        "plugin:prettier/recommended"
      ],
      "rules": {
        "react/forbid-prop-types": 0,
        "@typescript-eslint/explicit-function-return-type": 2,
        "@typescript-eslint/explicit-module-boundary-types": 2,
        "prettier/prettier": 1,
        // 使用prettier作为eslint规则
        "react-hooks/rules-of-hooks": "error",
        "react-hooks/exhaustive-deps": "warn",
        // 以上两行是精华,必须添加,使用钩子函数时才会提示警告/错误
        "react/jsx-filename-extension": [
          1,
          {
            "extensions": [
              ".ts",
              ".tsx"
            ]
          }
        ],
        "linebreak-style": 0,
        "react/prop-types": 0,
        "no-plusplus": [
          1,
          {
            "allowForLoopAfterthoughts": true
          }
        ],
        "react/react-in-jsx-scope": 0,
        "react/jsx-props-no-spreading": 0,
        "import/prefer-default-export": 0,
        "no-unused-expressions": [
          "error",
          {
            "allowShortCircuit": true,
            "allowTernary": true
          }
        ],
        "no-restricted-syntax": 0,
        "jsx-a11y/no-static-element-interactions": 0,
        "jsx-a11y/interactive-supports-focus": 0,
        "react/destructuring-assignment": 0,
        "jsx-a11y/click-events-have-key-events": 0,
        "@typescript-eslint/no-use-before-define": "off",
        "no-use-before-define": "off",
        "@typescript-eslint/ban-types": [
          "error",
          {
            "extendDefaults": true,
            "types": {
              "{}": false
            }
          }
        ],
        "@typescript-eslint/no-var-requires": 0,
        "react/display-name": 0
      }
    }
  ],
  "parserOptions": {
    "ecmaFeatures": {
      "jsx": true
    },
    "ecmaVersion": 6,
    "sourceType": "module"
  }
}

tsconfig配置

"allowJs": true, 
"checkJs": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,

注意以上属性的配置,否则eslint检查会对js使用ts规则

{
  "compilerOptions": {
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": false,
    "jsx": "react-jsx",
    "allowJs": true,
    "checkJs": false,
    "noImplicitAny": false
  },
  "include": [
    "./src/**/*.(ts|tsx)",
    "types"
  ],
  "exclude": [
    "node_modules"
  ]
}

package.json配置

{
    "@types/react": "^18.0.17",
    "@types/react-dom": "^18.0.6",
    "@typescript-eslint/eslint-plugin": "^5.6.0",
    "@typescript-eslint/parser": "^5.6.0",
    "ts-loader": "^9.3.1",
    "typescript": "^4.7.4",
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值