TypeScript · Middle · Question 67 of 75

How do you type a generator function?

function* ids(): Generator<number, void, unknown> {
  let i = 0;
  while (true) yield i++;
}

Explanation

The third parameter is the type of values passed to next(...). Async generators use AsyncGenerator<...>.

Further reading

Problem with a question

TypeScript · Middle · question 67

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz