[TS] React with TypeScript
- react-typescript-template @ pjchender github
- React and TypeScript @ front-end master
- React TypeScript CheatSheet
- typescript-cheatSheets/react @ GitHub
專案設置
建立專案
- 透過 create-react-app 產生專案
$ npx create-react-app my-app --template typescript
// 如果希望支援 React v17 不需要再寫 import React 的這個 feature
{
"jsx": "react-jsx" // 需要使用 "react-jsx" 而不能是 "react"
}
- 定義 eslintrc.js
$ npx eslint --init
# 安裝完 eslint 後可能會與 create-react-app 的 ESLint 有版本衝突
# 先移除 package.json 中的 eslint
$ rm package-lock.json
$ rm -rf node_modules
$ npm install
- 定義 .prettierrc
- 若要整合到 ESLint 需留意和 TypeScript 可能的衝突,可參考這篇的設定。
ESLint 設定
參考設定:create-exposed-app:eslint-config-airbnb-typescript 的參考設定。
React Error : is declared but its value is never read:參考 Introducing the New JSX Transform,升級到 react v17 後,不需要import React的 ESLint 設定
@typescript-eslint/eslint-plugin
- Getting Started - Linting your TypeScript Codebase:說明整個安裝設定流程 @ Github
- Getting Started - Linting with Type Information:讓 Lint 可以顯示型別的訊息 @ Github
錯誤處理:Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser

出現錯誤訊息:
Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: .eslintrc.js.
The file must be included in at least one of the projects provided