redux Typescript找不到还原

2wnc66cl  于 2022-11-12  发布在  TypeScript
关注(0)|答案(6)|浏览(132)

我收到以下错误:

node_modules/@types/react-redux/index.d.ts(8,24): error TS2307: Cannot find module 'redux'.

尽管已经安装了react-reduxreduxpackage.json):

"dependencies": {
    "react": "15.4.2",
    "react-native": "0.42.3",
    "react-redux": "^5.0.3",
    "redux": "^3.6.0"
},
"devDependencies": {
    "@types/react": "^15.0.18",
    "@types/react-native": "^0.42.9",
    "@types/react-redux": "^4.4.38",
    "@types/redux": "^3.6.0",
    "babel-jest": "19.0.0",
    "babel-preset-react-native": "1.9.1",
    "jest": "19.0.2",
    "react-test-renderer": "15.4.2",
    "tslint": "^4.5.1",
    "typescript": "^2.2.1"
},

@types/redux中的README.md文件表示:

This is a stub types definition for Redux (https://github.com/reactjs/redux).
Redux provides its own type definitions, so you don't need @types/redux installed!

但是卸载@types/redux包没有什么区别。
有什么想法吗?

更新

我以为通过在@types/redux目录(只包含export * from ../../redux/index)中添加一个index.d.ts,就可以解决问题,但没有任何乐趣。
下面是我的tsconfig.json

{
    "compilerOptions": {
        "target": "es2015",
        "module": "es2015",
        "jsx": "react",
        "outDir": "build",
        "rootDir": "src",
        "allowSyntheticDefaultImports": true,
        "noImplicitAny": true,
        "experimentalDecorators": true,
        "preserveConstEnums": true,
        "allowJs": true,
        "sourceMap": true
    },
    "filesGlob": [
        "src/**/*.ts",
        "src/**/*.tsx"
    ],
    "exclude": [
        "__tests__",
        "index.android.js",
        "index.ios.js",
        "build",
        "node_modules"
    ],
    "compileOnSave": false
}

而且我确实从node_modules中删除了@types/redux目录。我(显然)运行的是TypeScript 2. 2. 1。

更新2

使用redux的代码:

import { combineReducers } from "redux"

export default combineReducers({})

请注意,在"redux"后面添加分号没有任何帮助。

dced5bon

dced5bon1#

我在一个React原生项目中遇到过同样的问题。这个修复对我很有效:
您必须添加“moduleResolution”:“node”添加到编译器选项,以便包括package.json文件中链接的类型定义。
此处为文档https://www.typescriptlang.org/docs/handbook/module-resolution.html

nue99wik

nue99wik2#

我恢复到@types/redux@3.6.31,这样就解决了问题。这个版本包含了redux的index.d.ts文件。可能是TypeScript编译器的问题,它没有在正常的node_modules/redux目录中查找index.d.ts文件,而是只在node_module/@types下查找。

niwlg2el

niwlg2el3#

如果您在使用以下react版本"^16.8.6"时遇到相同的问题,请验证npm redux包是否

包.json

"dependencies": {
    "@types/jest": "24.0.13",
    "@types/node": "12.0.3",
    "@types/react": "16.8.19",
    "@types/react-dom": "16.8.4",
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-redux": "^7.0.3",
    "react-scripts": "3.0.1",
    "redux": "^4.0.1",
    "typesafe-actions": "^4.4.0",
    "typescript": "3.5.1"
  },

如果没有安装,只需执行npm install redux,不需要npm i @types/redux,因为Redux包提供了自己的类型定义,所以您不需要安装@types/redux

oprakyz7

oprakyz74#

我已经有了moduleResolution: 'node'@types/redux被删除了。但是当我删除node_modules并重新安装时,问题就消失了。:耸耸肩。

pvcm50d1

pvcm50d15#

面临着同样的问题。
通过重新安装redux并保存修复。
npm install redux --save

1sbrub3j

1sbrub3j6#

在安装redux工具包后,我也遇到了同样的问题。有趣的是,我不需要重新安装节点模块。对我来说,有效的方法是停止并重新启动live服务器--问题消失了!
1.在Mac上,在您的VScode终端上,按“Control + C”终止服务器
1.再次运行“npm start”

相关问题