typescript - Typescript 和 Redux:绑定元素“字符串”隐式具有“任何”类型
问题描述
在我的 Android 的 React Native 应用程序中,我有一个使用Redux
.
正如该线程中的“Hemant”所述,我也必须将导入为组件的操作传递给props
组件。现在我尝试正确输入操作,但它给了我错误:Binding element 'string' implicitly has an 'any' type
我不明白为什么。
这是代码:
imports ...
import {reduxAction} from '../../../store/searchBar/actions';
type Props = {
reduxAction: ({value: string}) => void;
};
const SearchBar: React.FC<Props> = ({reduxAction}) => {
// calling the action method
const sendNamesToReduxStore = (names: string) => {
// ... some other logic
// calling the action method
reduxAction({value: names});
};
如您所见,在调用 action 方法时,names
被声明为string
. 因此,我也将其声明为string
in Props
。我认为这是完全正确的。请注意:当我这样做reduxAction: ({value: any}) => void;
时,它会给出与上述相同的错误any
:Binding element 'any' implicitly has an 'any' type
。
你能告诉我我在这里做错了什么吗?
解决方案
如果其他人遇到这个问题:
您必须使用操作中声明的类型在函数中声明变量的类型。
reduxAction: (value: reduxActionState) => void;
...因为在actions.ts
我宣布
export const reduxAction = (value: reduxActionState): ResultValueType => ({
type: SET_RESULT_VALUE,
payload: value,
});
推荐阅读
- spring - DiscoveryClient_EMPLOYEE-SERVICE/localhost:employee-service:9090 - 注册失败无法在任何已知服务器上执行请求
- javascript - Sequelize.js includes returning incorrect data
- excel - 连接单元格并保持原始格式
- python - 从 TF 1.12 移动到 1.10 后密集层输出形状错误
- reactjs - 热门在反应中获得按钮的价值
- node.js - 关于 socket.io 中的房间创建
- nuxt.js - 如何正确安装 vue-tags-input
- memory - 确定给定地址和偏移量的虚拟和物理内存大小
- git - Git问题-检查我的用户/个人文件夹
- python - 如何在熊猫数据框中有一个空的第一行