我正在处理我的 typescript + redux 项目,并尝试从 redux 导入类型 Action,如下所示。
import { Action } from 'redux'
它显示了一个错误,它说,
TS2305: Module '"../../../../../../Users/LOCAL_DIR/node_modules/redux/es/redux"' has no exported member 'Action'.
但是,我看到 redux/index.d.ts
中有类型声明,当我这样写时它起作用了,
import { Action } from 'redux/index.d.ts'
根据这个例子,https://redux.js.org/recipes/usage-with-typescript#usage-with-redux-thunk ,它似乎可以与 import { Action } from 'redux'
一起使用,但在我的情况下不行。是否有任何额外的配置?
我已经在我的环境中安装了这些版本。
- "redux": "^4.0.4"
- "@types/redux": "^3.6.0"
这是我的 tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"skipLibCheck": true,
"module": "es2015",
"target": "es5",
"jsx": "react",
"strictNullChecks": true,
"allowJs": true,
"declaration": false,
"allowSyntheticDefaultImports": true,
"moduleResolution": "node",
"lib": [
"dom",
"es2015",
"es5",
"es6"
]
}
}
谢谢!
最佳答案
Redux 提供了它自己的类型定义,所以你不需要从 Definitely Typed 安装它们。如果您卸载 @types/redux
,TypeScript 应该使用 redux
包提供的那些,并且您的导入将起作用。
@types/redux
包已经deprecated .
关于reactjs - Typescript 找不到导入类型 Action,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57470757/