使用 TS 使用 React-hook-form 构建应用程序时出错

Sab*_*med 6 typescript reactjs react-hook-form

当我尝试在 TS 中使用 React-hook-form 构建 React 应用程序时,出现以下错误:

node_modules/react-hook-form/dist/types/utils.d.ts:24:77 - error TS1005: '?' expected.

24 declare type PathImpl<K extends string | number, V> = V extends Primitive ? `${K}` : `${K}` | `${K}.${Path<V>}`;
                                                                               ~~~

node_modules/react-hook-form/dist/types/utils.d.ts:24:84 - error TS1005: ';' expected.

24 declare type PathImpl<K extends string | number, V> = V extends Primitive ? `${K}` : `${K}` | `${K}.${Path<V>}`;
                                                                                      ~

node_modules/react-hook-form/dist/types/utils.d.ts:24:110 - error TS1005: '(' expected.

24 declare type PathImpl<K extends string | number, V> = V extends Primitive ? `${K}` : `${K}` | `${K}.${Path<V>}`;
Run Code Online (Sandbox Code Playgroud)

这是我的构建命令:

yarn tsc --emitDeclarationOnly && tsc-alias -p tsconfig.json
Run Code Online (Sandbox Code Playgroud)

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": false,
    "jsx": "react",
    "declaration": true,
    "outDir": "dist",
    "rootDir": "src",
    "downlevelIteration": true,
    "noImplicitAny": false,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "noEmit": false,
    "baseUrl": "./src",
    "paths": {
      "@context/*": [
        "context/*"
      ],
      "@types": [
        "types/index.ts"
      ]
    }
  },
  "include": [
    "src/**/*",
  ],
  "exclude": [
    "node_modules/",
    "./src/**/*.stories.tsx",
    "./src/**/*.test.tsx",
    "./src/**/__mocks__/**/*"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我正在使用react-hook-form版本7.6.6。任何有关如何解决此问题的建议将不胜感激!

小智 13

根据this讨论react-hook-formv7需要TS 4.1以上