Jon*_*dez 110 typescript reactjs eslint react-hooks
目前在我的客户端启动服务器,上面的错误是我得到的。我正在使用 Typescript、React、ESlint。我似乎无法前进,因为这个错误一直困扰着我。Eslint 的 github 页面也没有太大帮助。
在我创建 useMutation 组件并将其导出到 index.ts 后,这个错误出现了,不知道如何摆脱这个错误。
Below is my package.json
Run Code Online (Sandbox Code Playgroud)
{
"name": "tinyhouse_client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.35",
"@types/react-dom": "^16.9.0",
"@typescript-eslint/parser": "^3.0.2",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "3.4.1",
"typescript": "~2.23.0"
},
"resolutions": {
"@typescript-eslint/eslint-plugin": "^2.23.0",
"@typescript-eslint/parser": "^2.23.0",
"@typescript-eslint/typescript-estree": "^2.23.0"
},
"scripts": {
"start": "react-scripts start",
" build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
**strong text** "proxy": "http://localhost:9000"
}
Run Code Online (Sandbox Code Playgroud)
Below is my index.ts
Run Code Online (Sandbox Code Playgroud)
export * from './server';
export * from './useQuery';
export * from './useMutation';
Run Code Online (Sandbox Code Playgroud)
And my useMutation.ts
Run Code Online (Sandbox Code Playgroud)
import { useState } from 'react';
import { server } from './server';
interface State<TData> {
data: TData | null;
loading: boolean;
error: boolean;
}
type MutationTuple<TData, TVariables> = [
(variables?: TVariables | undefined) => Promise<void>,
State<TData>
];
export const useMutation = <TData = any, TVariables = any>(
query: string
): MutationTuple<TData, TVariables> => {
const [state, setState] = useState<State<TData>>({
data: null,
loading: false,
error: false,
})
const fetch = async (variables?: TVariables) => {
try {
setState({ data: null, loading: true, error: false });
const { data, errors } = await server.fetch<TData, TVariables>({ query, variables });
if (errors && errors.length) {
throw new Error(errors[0].message);
}
setState({ data, loading: false, error: false });
} catch (err) {
setState({ data: null, loading: false, error: true });
throw console.error(err);
}
}
return [fetch, state];
};
Run Code Online (Sandbox Code Playgroud)
Wil*_*den 84
编辑:正如 Meng-Yuan Huang 所指出的,这个问题不再发生在react-scripts@^4.0.1
出现此错误的原因react-scripts对的2.xx的范围内的直接依赖@typescript-eslint/parser和@typescript-eslint/eslint-plugin。
您可以通过向您添加分辨率字段来解决此问题,package.json如下所示:
"resolutions": {
"**/@typescript-eslint/eslint-plugin": "^4.1.1",
"**/@typescript-eslint/parser": "^4.1.1"
}
Run Code Online (Sandbox Code Playgroud)
NPM 用户:将上面的分辨率字段添加到您的package.json但使用npx npm-force-resolutions更新package-lock.json.
Yarn 用户:你不需要做任何其他事情。有关更多信息,请参阅选择性依赖项解决方案。
注意:如果您使用的是 monorepo/Yarn 工作区,则该resolutions字段必须位于顶级package.json.
注意:yarn add并且yarn upgrade-interactive不尊重该resolutions字段,因此他们可能会生成yarn.lock版本不正确的文件。小心。
Sei*_*Day 57
对于未来的 Google 员工:
我刚才在 Vue.js 2 项目中的 TypeScript 4.0.2 上遇到了同样的问题。我通过升级@typescript-eslint/eslint-plugin和@typescript-eslint/parsernpm 给我的最新版本来修复它,@latest当时分别是 3.3.0 和 3.10.1。
小智 40
尝试在接口中使用变量类型。例如,当我有这样的状态接口时,我遇到了这个错误:
interface State{
foo: []
}
Run Code Online (Sandbox Code Playgroud)
但是当我改变了数组的类型时,它起作用了:
interface State{
foo: string[]
}
Run Code Online (Sandbox Code Playgroud)
Bla*_*ack 32
您的 TypeScript 版本与您的 eslint 不兼容。您可以通过将这两个依赖项升级到最新版本来修复它。
TypeScript 4.0.5与 4.6.0 版本兼容
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.6.0",
"@typescript-eslint/parser": "^4.6.0",
}
Run Code Online (Sandbox Code Playgroud)
TypeScript 4.1.5与 4.18.0 版本兼容
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
}
Run Code Online (Sandbox Code Playgroud)
TypeScript 4.2.4与 4.23.0 版本兼容
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.23.0",
"@typescript-eslint/parser": "^4.23.0",
}
Run Code Online (Sandbox Code Playgroud)
TypeScript 4.3.2与 4.25.0 版本兼容
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^4.25.0",
"@typescript-eslint/parser": "^4.25.0",
}
Run Code Online (Sandbox Code Playgroud)
sun*_*sen 24
这对我的 CRA 项目有效。
第 1 步:编辑package.json并将typescript版本设置为^3.9.7
第二步:删除.cache文件夹node_modules
第 3 步:运行 npm install
小智 18
这是来自 eslint-typescript 吗?如果是这样,请检查您的打字稿版本是否不是开发/夜间构建。
DLe*_*Lee 13
有时此错误是其他人所说的无效类型的结果
使用裸数组作为类型时出现此错误
const someVariable: [] //Incorrect
const someVariable: string[] //Correct
Run Code Online (Sandbox Code Playgroud)
错误地输入多维数组时,我也收到此错误:
const someVariable : [string[]] //Incorrect. Results in map error
const someVariable : string[][] //Correct
Run Code Online (Sandbox Code Playgroud)
来自打字稿的错误消息非常神秘,所以希望这会有所帮助。
Jib*_*mas 12
我有同样的错误,但对我来说修复是我有一种类型
text : string[] | []
Run Code Online (Sandbox Code Playgroud)
并将其更改为
text : string[]
Run Code Online (Sandbox Code Playgroud)
为我工作
另外,除了array types上面描述的问题之外,还有很多tuple情况会导致这个问题。让我提一下
interface Some {
// error
tupleValue: [string, number]
// works
tupleValue: [v1: string, v2: number]
// error
tupleArr: [v1: string, v2: number][]
// works!
tupleArr2: Array<[v1: string, v2: number]>
}
Run Code Online (Sandbox Code Playgroud)
另一个案例:
type ValueView = [value: SomeObj, view: string]
// error
const res1: ValueView[] = arr.map(v => [v, v.name])
// works !!!
const res: ValueView[] = arr.map(v => [v, v.name] as ValueView)
// error
const res = arr.map(v => [v, v.name] as [SomeObj, string])
// works !!!
const res = arr.map(v => [v, v.name] as [value: SomeObj, view: string])
Run Code Online (Sandbox Code Playgroud)
所以检查你的元组操作两次
| 归档时间: |
|
| 查看次数: |
42855 次 |
| 最近记录: |