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

javascript - Redux Saga: Type 'unknown' is not assignable to type 'ServerResponse' - Stack Overf

programmeradmin2浏览0评论

Problem Statement:

I'm unable to specify type for the yield call(). The yield is calling an API that fetches some data from server. The of data type is ServerResponse. What I want to do is specify type as follows:

const response: ServerResponse = yield serverCall();

But I Typescript throws this error: Redux Saga: Type 'unknown' is not assignable to type 'ServerResponse'.

Code:

function* serverSaga(): Generator {
  try {
    // TS throws error here;
    const response: ServerResponse = yield serverCall();
    ...
    ...
  } catch (err) {
    ...
  }
}

const serverCall = async (): Promise<ServerResponse> => {
  try {
    const response =  await ...
    return response;
  } catch (err) {
    ...
  }
};

Problem Statement:

I'm unable to specify type for the yield call(). The yield is calling an API that fetches some data from server. The of data type is ServerResponse. What I want to do is specify type as follows:

const response: ServerResponse = yield serverCall();

But I Typescript throws this error: Redux Saga: Type 'unknown' is not assignable to type 'ServerResponse'.

Code:

function* serverSaga(): Generator {
  try {
    // TS throws error here;
    const response: ServerResponse = yield serverCall();
    ...
    ...
  } catch (err) {
    ...
  }
}

const serverCall = async (): Promise<ServerResponse> => {
  try {
    const response =  await ...
    return response;
  } catch (err) {
    ...
  }
};
Share Improve this question asked Jul 13, 2020 at 15:27 Vinay SharmaVinay Sharma 3,8176 gold badges38 silver badges69 bronze badges 4
  • Generator is a generic. You should be writing something like serverSage(): Generator<some, cool, types>{}. I forget which parameter is which. – zero298 Commented Jul 13, 2020 at 15:32
  • @zero298 would great if you can let me know about params
发布评论

评论列表(0)

  1. 暂无评论