如何在Typescript中修改这个箭头函数



我需要将这个箭头函数更改为我返回toto的常规函数,但我真的不明白如何,下面是代码:

class Ratchet extends APlugin<CentralizerConfigPluginRatchet> {
public search = (): Promise<CentralizerNotif[]> => {throw Error("Not implemented");};
public notif =
(): Promise<CentralizerNotif[]> => {
const toto = new RatchetLib.RatchetAPI(this.config.baseurl);
return toto.getAsset({validation: true}).then(result => {
return [{
name: `You ${result.assets.length} things to moderate `,
link: "https://www.exemple.com",
}];
});
};
}

有人能帮我吗?

我看不出您为什么需要将其更改为关键字函数。但如果你不想,你只需要删除==>:

class Ratchet extends APlugin<CentralizerConfigPluginRatchet> {
public search(): Promise<CentralizerNotif[]> {
throw Error("Not implemented");
}
public notif(): Promise<CentralizerNotif[]> {
const toto = new RatchetLib.RatchetAPI(this.config.baseurl);
return toto.getAsset({validation: true}).then(result => {
return [{
name: `You ${result.assets.length} things to moderate `,
link: "https://www.exemple.com",
}];
});
}
}

将匿名箭头回调函数更改为常规函数非常简单。在你的例子中,

return toto.getAsset({validation: true}).then(result => {
/* ... */
});

将成为

return toto.getAsset({validation: true}).then(function (result) {
/* ... */
});

你也可以选择在这里有一个命名函数:

return toto.getAsset({validation: true}).then(function myCustomFunc(result) {
/* ... */
});

请记住,箭头函数和函数声明(使用function关键字)有一些需要注意的差异。

最新更新