admin管理员组

文章数量:1429538

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

    本文标签: javascriptRedux Saga Type 39unknown39 is not assignable to type 39ServerResponse39Stack Overflow