Я использую Typescript с реакцией, и я пытаюсь создать компонент, используя Generi c в tsx . Когда я создавал компонент, моя IDE не жаловалась на синтаксис, и казалось, что все работает правильно, но затем, когда я пытаюсь запустить приложение, компилятор Typescript генерирует исключение в консоли, сообщая мне, что синтаксис не поддерживается, поэтому Я предполагаю, что проблема действительно связана с конфигурацией babel. Я не уверен, что это babel или webpack. Я пробовал другое решение, доступное в inte rnet, но ни одно из них пока не решило проблему.
Мой компонент:
function AutoComplete<T extends { id?: number | string, [key: string]: string | number | any }>(
{
...others
}: Props<T>
) {
return (
<div> My component</div>
)
}
Как я использую generi c в JSX
<AutoComplete
<IContact>
onSearch={handleSearch}
dataSource={contacts}/>
My tsconfig. json
{
"compilerOptions": {
"baseUrl": "src",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"lib": [
"esnext",
"dom",
"scripthost"
],
"jsx": "react",
"declaration": false,
"noUnusedLocals": true,
"noUnusedParameters": true,
"resolveJsonModule": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"allowJs": true
},
"include": [
"./src/**/*"
]
}
Мой eslintr c
{
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"modules": true,
"arrowFunctions": true,
"classes": true
},
"env": {
"es6": true,
"browser": true,
"node": true
}
},
"parser": "babel-eslint",
"plugins": [
"react",
"react-hooks",
"@typescript-eslint"
],
"rules": {
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn"
}
}
Мой пакет. json
{
"name": "supplier-dashboard-web",
"version": "0.1.0",
"private": true,
"dependencies": {
"@ant-design/compatible": "^1.0.2",
"@ant-design/icons": "^4.0.5",
"@bugsnag/js": "6.4.3",
"@bugsnag/plugin-react": "6.4.0",
"@svgr/webpack": "4.3.3",
"@types/node": "12.12.11",
"@types/react": "16.9.29",
"@types/react-dom": "16.9.4",
"@types/react-redux": "7.1.5",
"@types/react-router": "5.1.3",
"@types/react-router-dom": "5.1.2",
"@types/recharts": "^1.8.5",
"antd": "4.1.0",
"axios": "0.19.0",
"camelcase": "5.3.1",
"case-sensitive-paths-webpack-plugin": "2.2.0",
"convert-vapid-public-key": "^1.0.1",
"dotenv": "6.2.0",
"dotenv-expand": "5.1.0",
"file-loader": "3.0.1",
"fs-extra": "7.0.1",
"html-webpack-plugin": "4.0.0-beta.5",
"identity-obj-proxy": "3.0.0",
"is-wsl": "1.1.0",
"lodash-es": "^4.17.15",
"mini-css-extract-plugin": "0.8.0",
"node-sass": "4.13.0",
"optimize-css-assets-webpack-plugin": "5.0.3",
"pnp-webpack-plugin": "1.5.0",
"postcss-flexbugs-fixes": "4.1.0",
"postcss-loader": "3.0.0",
"postcss-normalize": "7.0.1",
"postcss-preset-env": "6.7.0",
"postcss-safe-parser": "4.0.1",
"react": "16.12.0",
"react-app-polyfill": "1.0.4",
"react-currency-input": "^1.3.6",
"react-dev-utils": "9.1.0",
"react-device-detect": "1.9.10",
"react-dom": "16.12.0",
"react-intl": "3.7.0",
"react-redux": "7.1.3",
"react-router": "5.1.2",
"react-router-dom": "5.1.2",
"recharts": "^1.8.5",
"redux": "4.0.4",
"redux-devtools-extension": "^2.13.8",
"redux-thunk": "2.3.0",
"resolve": "1.12.2",
"resolve-url-loader": "3.1.1",
"sass-loader": "7.2.0",
"semver": "6.3.0",
"style-loader": "1.0.0",
"terser-webpack-plugin": "1.4.1",
"ts-pnp": "1.1.4",
"url-loader": "2.1.0",
"webpack": "4.41.2",
"webpack-manifest-plugin": "2.2.0",
"workbox-webpack-plugin": "4.3.1"
},
"resolutions": {
"@types/react": "16.9.29"
},
"size-limit": [
{
"limit": "700 KB",
"path": "./build/static/js/*.js",
"webpack": true,
"running": false
}
],
"scripts": {
"start": "node scripts/start.js",
"build": "node scripts/build.js",
"build:production": "NODE_ENV=production node scripts/build.js",
"build:staging": "NODE_ENV=staging node scripts/build.js",
"build:training": "NODE_ENV=training node scripts/build.js",
"test": "REACT_APP_ENV=test jest --runInBand && eslint .",
"test:watch": "npm test -- --watch",
"dev": "npm run start",
"size": "npm run build:production && npx size-limit",
"analyze": "source-map-explorer build/static/js/*.js"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"jest": {
"roots": [
"<rootDir>/src"
],
"collectCoverageFrom": [
"src/**/*.{js,jsx,ts,tsx}",
"!src/**/*.d.ts"
],
"resolver": "jest-pnp-resolver",
"setupFiles": [
"react-app-polyfill/jsdom",
"core-js"
],
"setupFilesAfterEnv": [
"<rootDir>/config/jest/setupTest.js"
],
"testMatch": [
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
],
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest",
"^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
"^(?!.*\\.(js|jsx|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
},
"transformIgnorePatterns": [
"[/\\\\]node_modules[/\\\\].+\\.(js|jsx|ts|tsx)$",
"^.+\\.module\\.(css|sass|scss)$"
],
"modulePaths": [
"src"
],
"moduleNameMapper": {
"^react-native$": "react-native-web",
"^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
},
"moduleFileExtensions": [
"web.js",
"js",
"web.ts",
"ts",
"web.tsx",
"tsx",
"json",
"web.jsx",
"jsx",
"node"
],
"watchPlugins": [
"jest-watch-typeahead/filename",
"jest-watch-typeahead/testname"
],
"browser": true
},
"babel": {
"presets": [
[
"@babel/preset-env"
],
"react-app"
],
"plugins": [
"@babel/plugin-proposal-optional-chaining"
]
},
"devDependencies": {
"@babel/core": "7.7.2",
"@babel/plugin-proposal-optional-chaining": "7.6.0",
"@babel/plugin-transform-typescript": "^7.9.4",
"@size-limit/preset-app": "^3.0.0",
"@size-limit/webpack": "^3.0.0",
"@testing-library/jest-dom": "4.2.4",
"@testing-library/react": "9.3.2",
"@types/jest": "^24.0.23",
"@types/lodash-es": "^4.17.3",
"@types/moxios": "^0.4.9",
"@types/redux-mock-store": "^1.0.2",
"@typescript-eslint/eslint-plugin": "2.8.0",
"@typescript-eslint/parser": "2.8.0",
"babel-eslint": "10.0.3",
"babel-jest": "^24.9.0",
"babel-loader": "8.0.6",
"babel-plugin-import": "1.13.0",
"babel-plugin-named-asset-import": "0.3.4",
"babel-preset-react-app": "9.0.2",
"css-loader": "2.1.1",
"eslint": "6.6.0",
"eslint-loader": "3.0.2",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.16.0",
"eslint-plugin-react-hooks": "1.7.0",
"jest": "^24.9.0",
"jest-environment-jsdom-fourteen": "0.1.0",
"jest-resolve": "24.9.0",
"jest-watch-typeahead": "0.4.2",
"less": "^3.10.3",
"less-loader": "^5.0.0",
"moxios": "^0.4.0",
"redux-mock-store": "^1.5.3",
"ts-jest": "^25.1.0",
"typescript": "^3.7.2",
"webpack-dev-server": "3.2.1"
}
}