简体   繁体   English

具有收益返回的静态生成器函数的正确打字稿语法是什么?

[英]what is the correct typescript syntax for a static generator function with a yield return?

I am trying to write a static function on a class, which uses the generator/yield pattern.我正在尝试在一个类上编写一个静态函数,它使用生成器/收益模式。 Can somebody give me a code example of what the method signature syntax looks like?有人可以给我一个方法签名语法的代码示例吗? Specifically, I am trying to write a fibonacci generator, but any number generator will do.具体来说,我正在尝试编写一个斐波那契生成器,但任何数字生成器都可以。

class Mathy {
    static yieldFibonacci(maxValue: number = Number.POSITIVE_INFINITY): IterableIterator<number> {
        let current = 0
        let next = 1
        while (next < maxValue) {
            yield next
            [(current, next)] = [next, current + next]
        }
    }
}
class Mathy {
    static *yieldFibonacci(maxValue: number = Number.POSITIVE_INFINITY): IterableIterator<number> {
        let current = 0
        let next = 1
        while (next < maxValue) {
            yield next
            ;[current, next] = [next, current + next]
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM