前提・実現したいこと
react-autosuggetionsを導入したいと思っています。
自分の環境はtypescriptなので書き換えたものを利用したいのですが、
以下のようなエラーが出て困っています。
valueの変数名を変えるとそのようなtypeはないと言われるので、ルールをoffにしようかなと思い、
調べてみたところ、.eslintrc.jsにルールを追加すればいけるらしいのですが、うまくいきませんでした。
わかる方がいましたら、コメントお願いします。
####参考
https://codesandbox.io/s/zx4ht?file=/src/index.tsx:0-1517
発生している問題・エラーメッセージ
'value' is already declared in the upper scope @typescript-eslint/no-shadow
該当のソースコード
import React from "react"; import { useState } from "react"; import ReactDOM from "react-dom"; import AutoSuggest from "react-autosuggest"; import "./styles.css"; const companies: string[] = [ "Company1", "Company2", "Big Corp", "Happy Toy Company" ]; const lowerCasedCompanies = companies.map(language => language.toLowerCase()); const App: React.FC = () => { const [value, setValue] = useState(""); const [suggestions, setSuggestions] = useState<string[]>([]); function getSuggestions(value: string): string[] { return lowerCasedCompanies.filter(language => language.startsWith(value.trim().toLowerCase()) ); } return ( <div> <AutoSuggest suggestions={suggestions} onSuggestionsClearRequested={() => setSuggestions([])} onSuggestionsFetchRequested={({ value }) => { setValue(value); setSuggestions(getSuggestions(value)); }} onSuggestionSelected={(_, { suggestionValue }) => console.log("Selected: " + suggestionValue) } getSuggestionValue={suggestion => suggestion} renderSuggestion={suggestion => <span>{suggestion}</span>} inputProps={{ placeholder: "Type 'c'", value: value, onChange: (_, { newValue, method }) => { setValue(newValue); } }} highlightFirstSuggestion={true} /> </div> ); }; const rootElement = document.getElementById("root"); ReactDOM.render(<App />, rootElement);
試したこと
.eslintrc.js
js
1module.exports = { 2 env: { 3 browser: true, 4 es2021: true, 5 }, 6 extends: [ 7 'plugin:react/recommended', 8 'airbnb', 9 'plugin:@typescript-eslint/recommended', 10 'plugin:prettier/recommended', 11 ], 12 globals: { 13 Atomics: 'readonly', 14 SharedArrayBuffer: 'readonly', 15 }, 16 parser: '@typescript-eslint/parser', 17 parserOptions: { 18 ecmaFeatures: { 19 jsx: true, 20 }, 21 ecmaVersion: 12, 22 sourceType: 'module', 23 }, 24 settings: { 25 'import/resolver': { 26 node: { 27 extensions: ['.js', '.jsx', '.ts', '.tsx'], 28 }, 29 }, 30 }, 31 plugins: ['react', '@typescript-eslint'], 32 rules: { 33 '@typescript-eslint/indent': ['error', 2], 34 '@typescript-eslint/prefer-interface': 'off', 35 'react/jsx-filename-extension': ['error', { extensions: ['.jsx', '.tsx'] }], 36 'react/prop-types': 'off', 37 'spaced-comment': ['error', 'always', { markers: ['/ <reference'] }], 38 'no-use-before-define': 'off', 39 '@typescript-eslint/no-use-before-define': ['error', { variables: false }], 40 'import/extensions': [ 41 'error', 42 'ignorePackages', 43 { 44 js: 'never', 45 jsx: 'never', 46 ts: 'never', 47 tsx: 'never', 48 }, 49 ], 50 'prettier/prettier': [ 51 'warn', 52 { 53 singleQuote: true, 54 semi: false, 55 }, 56 ], 57 //追加した箇所 58 // 'overrides': [ 59 // { 60 // 'rules': { // Here it worked 61 // '@typescript-eslint/no-shadow': ['error'], 62 // 'no-shadow': 'off', 63 // } 64 // } 65 // ] 66 //'no-shadow': 'off', 67 //'@typescript-eslint/no-shadow': ['error'], 68 }, 69} 70
###コメントに対して
package.json
{ "name": "catwiki_front", "version": "0.1.0", "private": true, "dependencies": { "@chakra-ui/icons": "^1.0.13", "@chakra-ui/react": "^1.6.0", "@emotion/react": "^11.1.5", "@emotion/styled": "^11.3.0", "@testing-library/jest-dom": "^5.12.0", "@testing-library/react": "^11.2.6", "@testing-library/user-event": "^12.8.3", "@types/jest": "^26.0.23", "@types/node": "^12.20.10", "@types/react": "^17.0.4", "@types/react-autosuggest": "^10.1.4", "@types/react-dom": "^17.0.3", "@types/react-router-dom": "^5.1.7", "framer-motion": "^4.1.11", "react": "^17.0.2", "react-autosuggest": "^10.1.0", "react-dom": "^17.0.2", "react-masonry-css": "^1.0.16", "react-router-dom": "^5.2.0", "react-scripts": "4.0.3", "typescript": "^4.2.4", "web-vitals": "^1.1.1" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "@typescript-eslint/eslint-plugin": "^4.22.0", "@typescript-eslint/parser": "^4.22.0", "eslint": "^7.25.0", "eslint-config-airbnb": "^18.2.1", "eslint-config-prettier": "^8.3.0", "eslint-plugin-import": "^2.22.1", "eslint-plugin-jsx-a11y": "^6.4.1", "eslint-plugin-prettier": "^3.4.0", "eslint-plugin-react": "^7.23.2", "eslint-plugin-react-hooks": "^4.2.0", "prettier": "^2.2.1" } }
例えばvalueをvalue_にすると以下のようにエラーが出力されます。
SuggestionsFetchRequestedParamsを自分で定義していない(@type/react-autosuggestで定義している?)ので、どのように対応したらいいのかわかりません。
プロパティ 'value_' は型 'SuggestionsFetchRequestedParams' に存在しません。
回答1件
あなたの回答
tips
プレビュー