close

[Solved] ‘yield’ expression implicitly results in an ‘any’ type because its containing generator lacks a return-type annotation

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error ‘yield’ expression implicitly results in an ‘any’ type because its containing generator lacks a return-type annotation in Javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How To Solve ‘yield’ expression implicitly results in an ‘any’ type because its containing generator lacks a return-type annotation Error ?

  1. How To Solve 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation Error?

    To Solve 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation Error In recent updates of typescript, there are more type restrictions on generator functions.

  2. 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation

    To Solve 'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation Error In recent updates of typescript, there are more type restrictions on generator functions.

Solution 1

I just solved this error by just doing this

export interface ResponseGenerator{
    config?:any,
    data?:any,
    headers?:any,
    request?:any,
    status?:number,
    statusText?:string
}
const response:ResponseGenerator = yield YOUR_YIELD_FUNCTION
console.log(response.data)

Solution 2

In recent updates of typescript, there are more type restrictions on generator functions.

Type 1: yield with call

function* initDashboard(): any {
  let response = yield call(getDashboardData);
  console.log(response);
}

Type 2: yield without call

function* initDashboard() {
  let response: any = yield getDashboardData;
  console.log(response);
}

Note: Using any is fastest solution but proper solution would be creating type/interface for the response and using it as type.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment