我得到以下错误:

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

尽管安装了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"
},

@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软件包没有什么区别.

有什么 idea 吗?

UPDATE

我认为,在@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.

UPDATE 2

代码使用redux:

import { combineReducers } from "redux"

export default combineReducers({})

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

推荐答案

我在一个项目中也遇到了同样的问题.这个方法对我有效:

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

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

Typescript相关问答推荐

带脉轮UI的TypScript支持StyleProps的正确方法是什么?

如何在同一页面中使用布局和提供程序?

如何准确确定此特定场景中的类型?

Tailwind CSS样式不在Svelte应用程序中呈现

如何传递基于TypScript中可变类型的类型?

Angular 17 Select 错误core.mjs:6531错误错误:NG 05105:发现意外合成监听器@transformPanel.done

接口的额外属性作为同一接口的方法的参数类型'

从typescript中的对象数组中获取对象的值作为类型'

contextPaneTitleText类型的参数不能分配给remoteconfig类型的关键参数'""'''

如何在Typescript 中输入两个相互关联的变量?

TypeScrip-将<;A、B、C和>之一转换为联合A|B|C

在包含泛型的类型脚本中引用递归类型A<;-&B

TypeScrip界面属性依赖于以前的属性-针对多种可能情况的简明解决方案

我可以使用TypeScrip从字符串词典/记录中填充强类型的环境对象吗?

类型脚本满足将布尔类型转换为文字.这正常吗?

在保留类型的同时进行深层键名转换

在Cypress中,是否可以在不标识错误的情况下对元素调用.Click()方法?

为什么数字是`{[key:string]:UNKNOWN;}`的有效密钥?

类型脚本中函数重载中的类型推断问题

显式推断对象为只读