首页 > 解决方案 > 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. 因此,我也将其声明为stringin Props。我认为这是完全正确的。请注意:当我这样做reduxAction: ({value: any}) => void;时,它会给出与上述相同的错误anyBinding element 'any' implicitly has an 'any' type

你能告诉我我在这里做错了什么吗?

标签: typescriptredux

解决方案


如果其他人遇到这个问题:

您必须使用操作中声明的类型在函数中声明变量的类型。

reduxAction: (value: reduxActionState) => void;

...因为在actions.ts我宣布

export const reduxAction = (value: reduxActionState): ResultValueType => ({
  type: SET_RESULT_VALUE,
  payload: value,
});

推荐阅读