RuntimeError
RuntimeError

Reputation: 1390

Typescript and Redux: Binding element 'string' implicitly has an 'any' type

In my React Native application for Android, I have a component that uses Redux.

As stated by "Hemant" in this Thread, I have to pass the action that is imported as props into the component, too. Now I try to type the action correctly, but it gives me the error: Binding element 'string' implicitly has an 'any' type and I don't understand why.

Here is the code:

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});
};

As you can see, when calling the action method, names is declared as string. Therefore, I declare it as string in Props, too. I thought this was absolutely right. Please note: When I do reduxAction: ({value: any}) => void; it gives the same error as above with any: Binding element 'any' implicitly has an 'any' type.

Can you please tell me what I am doing wrong here?

Upvotes: 1

Views: 611

Answers (1)

RuntimeError
RuntimeError

Reputation: 1390

In case someone else struggles with this problem:

You have to declare the type of the variable in your function with the type stated in the action.

reduxAction: (value: reduxActionState) => void;

... because in actions.ts I declared

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

Upvotes: 1

Related Questions