Yield expression implicitly results in an ‘any’ type in TypeScript – How to fix it?

Yield expression implicitly results in an ‘any’ type in TypeScript

Are you getting the error “Yield expression implicitly results in an ‘any’ type” in TypeScript and don’t know how to fix it? Don’t worry because in this tutorial, we will show you the cause of this problem and how to solve it properly. 

What causes this error?

Generator has been a very important feature in TypeScript since it was released. Yield is a keyword that is used to pause and resume the generator function. 

Syntax: 

yield [expression]

Parameter:

  • expression: the value to be returned from the generated function. 

Example 1: 

function* lineGenerator() {
    
    console.log('This is line 1!');
    yield 1; //value=1

    console.log('This is line 2!');
    yield 2; //value=2

    console.log('This is line 3!');
    yield 3; //value=3

    console.log('This is line 4');
    yield 4; //value=4
}

var line = lineGenerator(); 
line.next();
line.next();
line.next();
line.next();

Output: 

This is line 1!
This is line 2!
This is line 3!
This is line 4!

Reproduce the “Yield expression implicitly results in an ‘any’ type” error in TypeScript

Let’s take a look at example 2: 

function* myGenerator() {
    var myStr = yield 'LearnShareIT.com';
};

var result = myGenerator();
console.log(result.next());

Output: 

'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation.
'myStr' is declared but its value is never read.

As we run the program, we will have this error. This is because TypeScript cannot identify the type to be received from the expression. So, here are some solutions for this issue:

How to fix the error “Yield expression implicitly results in an ‘any’ type” in TypeScript?

Set the type of the generator function to ‘any’

One quick way to fix this is to set the type of our generator function to any

function* myGenerator() : any {
    var myStr = yield 'LearnShareIT.com';
};

var result = myGenerator();
console.log(result.next());

Output:

{
  "value": "LearnShareIT.com",
  "done": false
} 

However, this method is not highly recommended since what it does is turning off the type-checking, or we can say that, it tricks the compiler. 

Set the return-type annotation for the generator

Instead of setting the type of the generator to any, we can set the return-type annotation.

function* myGenerator(): Generator<string> {
    var myStr = yield 'LearnShareIT.com';
};

var result = myGenerator();
console.log(result.next());

Output:

{
  "value": "LearnShareIT.com",
  "done": false
}

The type that we provide to the generator is the type that we want to yield. So in our example, it will be string.

Not using the result 

As we can see in example 1, we are not using the results of yield, so there is no error. Therefore, if we don’t use the results, we don’t have to worry much about their type.

Summary

In this tutorial, we have shown you some of the solutions to fix the error “Yield expression implicitly results in an ‘any’ type” in TypeScript. The idea is that we can set the type of the generator to any or the exact return-type annotation. Let’s try it.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *