如何获取生成器的值

How to get value of generator

scenario.ts

export interface Step {
    text: string;
    image: string;
    time: number
}

export interface IScenario {
    name: string;

    steps: Array<Step>;
}

Restaurant.ts

import {IScenario, Step} from './interfaces/scenario'

export default class Restaurant implements IScenario {
    name: string = 'Приключение в ресторане';

    steps: Array<Step> = [
        {
            text: 'Вы пришли в ресторан',
            image: 'https://sun9-3.userapi.com/55H3n5pt-TvwwdQzmpBZ9mcHURCqf85x1mXvlw/oyI4OlFu3R0.jpg',
            time: 2000,
        },
        {
            text: 'Перед вами стоит стол',
            image: '',
            time: 5000,
        },
    ];

    private *enumerateSteps(steps: Array<Step>): IterableIterator<Step> {
        yield step;
    }   

    start(): void {
        for (const step of this.enumerateSteps(this.steps)) {
            setTimeout(() => {
                return console.log(step.text);
                step.next();
              }, step.time);
        }
    }
}

我想获取时间后的下一步,但是报错:

Property 'next' does not exist on type 'Step'.

而且由于某种原因,我也有关于 step 缺失的错误,但我不知道为什么。

你根本不应该在这里使用生成器。您可以通过 callback-based 方法

来完成您想要的
start(callback?: () => void): void {
    this.steps.reduceRight((next, step) => () => {
        console.log(step.text);
        setTimeout(next, step.time);
    }, () => {
        console.log('All steps done');
        callback?.();
    })();
}

或承诺和 async/await:

async start(): Promise<void> {
    for (const step of this.steps) {
        console.log(step.text);
        await new Promise(resolve => setTimeout(resolve, step.time));
    }
    console.log('All steps done');
}