打字稿找不到redux

时间:2017-03-24 15:30:29

标签: typescript redux react-redux

我收到以下错误:

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

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!

但卸载@types/redux包没有任何区别。

有什么想法吗?

更新

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

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

我确实从@types/redux删除了node_modules目录。我(显然)正在运行TypeScript 2.2.1。

更新2

使用redux的代码:

import { combineReducers } from "redux"

export default combineReducers({})

请注意,在"redux"之后添加分号无效。

4 个答案:

答案 0 :(得分:56)

我在反应原生项目中遇到了同样的问题。这个修复对我有用:

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

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

答案 1 :(得分:6)

我恢复了@types/redux@3.6.31并解决了这个问题。此版本包含redux的index.d.ts文件。 TypeScript编译器可能存在问题,即在index.d.ts目录中没有查找node_modules/redux文件,只能在node_module/@types下查找。

答案 2 :(得分:0)

我已经有#viz libs from wordcloud import WordCloud, STOPWORDS import matplotlib.pyplot as plt #img libs from PIL import Image #binary array lib import numpy as np #pdf reader import PyPDF4 pdfFileObj = open('Test-Resume-Doc.pdf', 'rb') pdfReader = PyPDF4.PdfFileReader(pdfFileObj) print(pdfReader.numPages) pageObj = pdfReader.getPage(0) pageText = (pageObj.extractText()) pdfFileObj.close() #set stopwords stopwords = set(STOPWORDS) #can call stopwords from a list as such #stopwords.update(["word1", "word2", "word3", ...]) #call stopwords from txt file and program executes ignoring txt file, the problem is how the path is run stopwords.update(['stopwords.txt']) rsMask = np.array(Image.open('Resume_WordCloud.png')) #create wordcloud with stopwords cloud = WordCloud(stopwords=stopwords, background_color="black", mask=rsMask).generate(pageObj.extractText()) plt.imshow(cloud, interpolation="bilinear") plt.axis("off") plt.savefig('path.../PythonPDFRW/Resume_WordCloud_fromPython.png'.format(cloud)) plt.show()``` ,并且moduleResolution: 'node'已被删除。但是当我删除@types/redux并重新安装时,问题就消失了。 :耸耸肩。

答案 3 :(得分:0)

如果您使用以下反应版本"^16.8.6"遇到相同的问题,请验证npm redux软件包

Package.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