Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  120] [ 1]  / answers: 1 / hits: 25814  / 3 Years ago, wed, january 13, 2021, 12:00:00

I got type this problem with the following code:


"Argument of type '(response: IResponse) => void' is not assignable to parameter of type '(value: void) => void | PromiseLike'".


And problem this the check() function.


Could you please help me?
I've got really no ideas how to fix.
Thanks!




interface IResponse {
isChecked: boolean;
}
type TResponse = IResponse;

.....

function dispatchAsync<TResponsePayload>(dispatch: Dispatch, actionType: string, asyncCall: () => Promise<TResponsePayload>): any;

....

check = async () => {
const {actions, param} = this.props;
await actions.getInfoById(param.id).then(
(response: IResponse) => {
this.setState({isChecked: response.isChecked});
}
);
};

.....

getInfoById = async (id: string): Promise<void> =>
dispatchAsync<TPermission>(this.dispatch, CHECK_ACTION, async () => {
return await this.service.getInfoById(id);
});




More From » typescript

 Answers
7

To explain a little bit more, the TypeScript error tells you in another words that you cannot put a function that expects an argument in place of a function that doesn't expect any argument whatsoever.
Imagine code like this:




const myArg = (param: number) => setState(param + 5);

const caller = (arg: () => void) => arg();

caller(myArg);




Here is pretty obvious the caller won't provide any arguments when calling it's argument. Therefore calling the caller with myArg will result in an error thrown inside the myArg as it won't receive the argument it expects.


In your example the getInfoById returns Promise<void>. That means the then method will not provide any argument to the provided callback. That is the root cause.
You can fix it by telling the TypeScript that getInfoById actually returns something inside the promise (in this case I assume it is the IResponse type).
Like so:




getInfoById = async (id: string): Promise<IResponse> =>
dispatchAsync<TPermission>(this.dispatch, CHECK_ACTION, async () => {
return await this.service.getInfoById(id);
});




[#50456] Wednesday, December 16, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
alejandro

Total Points: 231
Total Questions: 102
Total Answers: 107

Location: Jordan
Member since Wed, Jun 17, 2020
4 Years ago
alejandro questions
Mon, Jul 18, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
Thu, Sep 10, 20, 00:00, 4 Years ago
;