我对打字稿更有新鲜感,当我从网站上学习的时候,我知道产量可以用于异步迭代,使用的是等待-等待。以下是Javascript中的函数。请帮助我如何在打字本课上使用。当我编写下面的代码时,我得到的错误是TS1163:只有在生成器体中才允许“产生”表达式。--我想在类型记录类中编写下面的代码。
https://blog.bitsrc.io/keep-your-promises-in-typescript-using-async-await-7bdc57041308。
function* numbers() {
let index = 1;
while(true) {
yield index;
index = index + 1;
if (index > 10) {
break;
}
}
}
function gilad() {
for (const num of numbers()) {
console.log(num);
}
}
gilad();
我还试着在一个打字稿课上写东西,但是它给出了编译问题。
public getValues(): number {
let index = 1;
while(true) {
yield index;
index = index + 1;
if (index > 10) {
break;
}
}
}
发布于 2020-07-26 09:20:23
您需要将令牌*
放在您的方法前面:
class X {
public *getValues() { // you can put the return type Generator<number>, but it is ot necessary as ts will infer
let index = 1;
while(true) {
yield index;
index = index + 1;
if (index > 10) {
break;
}
}
}
}
https://stackoverflow.com/questions/63093193
复制相似问题