打字稿找不到redux [英] Typescript cannot find redux
问题描述
我收到以下错误:
node_modules/@types/react-redux/index.d.ts(8,24): error TS2307: Cannot find module 'redux'.
尽管安装了 react-redux
和 redux
(package.json
):
despite having installed both react-redux
and redux
(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"
},
@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
包没有任何区别.
but uninstalling the @types/redux
package makes no difference.
有什么想法吗?
更新
我认为通过将 index.d.ts
添加到 @types/redux
目录(仅包含 export * from ../../redux/index
),它会解决问题,但没有乐趣.
I thought that by adding an index.d.ts
to the @types/redux
directory (containing only export * from ../../redux/index
), it would solve the problem, but no joy.
这是我的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.
and I definitely deleted the @types/redux
directory from node_modules
. I am (obviously) running TypeScript 2.2.1.
更新 2
使用redux
的代码:
import { combineReducers } from "redux"
export default combineReducers({})
请注意,在 "redux"
后添加分号没有帮助.
Note that adding a semicolon after "redux"
does not help.
推荐答案
我在 react-native 项目中遇到了同样的问题.此修复程序对我有用:
I experienced the same problem on a react-native project. This fix worked for me:
您必须将 "moduleResolution": "node" 添加到编译器选项中,以便包含链接到 package.json 文件中的类型定义.
You have to add "moduleResolution": "node" to the compiler options in order to include type definitions linked in package.json files.
此处的文档https://www.typescriptlang.org/docs/handbook/模块分辨率.html
这篇关于打字稿找不到redux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!