typescript 打字稿找不到redux

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43003491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:23:06  来源:igfitidea点击:

Typescript cannot find redux

typescriptreduxreact-redux

提问by Ralph

I am getting the following error:

我收到以下错误:

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

despite having installed both react-reduxand redux(package.json):

尽管同时安装了react-reduxredux( package.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"
},

The README.mdfile in @types/reduxsays:

里面的README.md文件@types/redux说:

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!

but uninstalling the @types/reduxpackage makes no difference.

但卸载@types/redux软件包没有区别。

Any ideas?

有任何想法吗?

UPDATE

更新

I thought that by adding an index.d.tsto the @types/reduxdirectory (containing only export * from ../../redux/index), it would solve the problem, but no joy.

我认为通过index.d.ts@types/redux目录中添加一个(仅包含export * from ../../redux/index),它会解决问题,但没有乐趣。

Here is my tsconfig.json:

这是我的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
}

and I definitely deleted the @types/reduxdirectory from node_modules. I am (obviously) running TypeScript 2.2.1.

我确实@types/reduxnode_modules. 我(显然)正在运行 TypeScript 2.2.1。

UPDATE 2

更新 2

Code using redux:

代码使用redux

import { combineReducers } from "redux"

export default combineReducers({})

Note that adding a semicolon after "redux"does not help.

请注意,在后面添加分号"redux"无济于事。

回答by Andrew Sinner

I experienced the same problem on a react-native project. This fix worked for me:

我在 react-native 项目中遇到了同样的问题。这个修复对我有用:

You have to add "moduleResolution": "node" to the compiler options in order to include type definitions linked in package.json files.

您必须将 "moduleResolution": "node" 添加到编译器选项中,以便包含在 package.json 文件中链接的类型定义。

Documentation here https://www.typescriptlang.org/docs/handbook/module-resolution.html

此处的文档https://www.typescriptlang.org/docs/handbook/module-resolution.html

回答by Ralph

I reverted to @types/[email protected]and that resolved the problem. This version includes the index.d.tsfile for redux. There might be a problem with the TypeScript compiler not looking for the index.d.tsfile in the normal node_modules/reduxdirectory, but only under node_module/@types.

我恢复到@types/[email protected]并解决了问题。此版本包含index.d.tsredux的文件。TypeScript 编译器可能有问题,没有index.d.ts在普通node_modules/redux目录中查找文件,而只是在node_module/@types.

回答by BRAHIM Kamel

If you encounter the same issue with the following react version "^16.8.6"verify if the npm redux package

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

Package.json

包.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"
  },

if not installed just do npm install reduxno need for npm i @types/reduxas Redux package provides its own type definitions, so you don't need @types/reduxinstalled!

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

回答by Jeremy John

I already had moduleResolution: 'node'and @types/reduxwas deleted. But when I deleted node_modulesand reinstalled, the problem went away. :shrug.

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