最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript error Argument of type '(response) => void' is not assignable to parameter of typ

programmeradmin3浏览0评论

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

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

Share Improve this question asked Jan 13, 2021 at 3:34 SonneSonne 1171 gold badge1 silver badge4 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

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

You can look at your code below:

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

You can change void in your Promise<void> with any or your spesific data type.

Because you can use return if you use void.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论