使用替换功能回调时的编译错误时的编译错误
我有一个生成随机单词的函数:
const randomWord = () => {
return "obbooobb".replace(/([ob])/g, (match: string) => {
if (match === "o") {
// something
}
if (match === "b") {
// something
}
});
};
Anytime DENO
在开始运行时启动检查以下错误:
Check file:///root/index.ts
error: TS2769 [ERROR]: No overload matches this call.
The last overload gave the following error.
Argument of type '(match: string) => string | undefined' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
return "obbooobb".replace(/([ob])/g, (match: string) => {
~~~~~~~~~~~~~~~~~~~~
at file:///root/controllers/data.ts:66:60
TS2771 [ERROR]: The last overload is declared here.
replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at asset:///lib.es5.d.ts:456:5
- DENO 1.21.2(发布,X86_64-INGNOWN-LINUX-GNU)
- v8 10.0.139.17
- typespript typesscript 4.6。 2
从初始搜索开始,我或多或少都知道这已经存在。我看到了其他stackoverflow
和git问题
与此问题相关的主题,但是我没有找到最简单的方法来解决此问题可以帮助我的问题,也许有人已经遇到了类似的问题和以简单的方式解决了它。
I have a function that generates random word:
const randomWord = () => {
return "obbooobb".replace(/([ob])/g, (match: string) => {
if (match === "o") {
// something
}
if (match === "b") {
// something
}
});
};
Anytime deno
launches check before starting runtime it throws following error:
Check file:///root/index.ts
error: TS2769 [ERROR]: No overload matches this call.
The last overload gave the following error.
Argument of type '(match: string) => string | undefined' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
Type 'string | undefined' is not assignable to type 'string'.
Type 'undefined' is not assignable to type 'string'.
return "obbooobb".replace(/([ob])/g, (match: string) => {
~~~~~~~~~~~~~~~~~~~~
at file:///root/controllers/data.ts:66:60
TS2771 [ERROR]: The last overload is declared here.
replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
at asset:///lib.es5.d.ts:456:5
- deno 1.21.2 (release, x86_64-unknown-linux-gnu)
- v8 10.0.139.17
- typescript 4.6.2
From initial search i am more or less aware that this has been issue. I seen other stackoverflow
and git issues
topics related to this problem, but i didnt find most simple way to solve this issue that would help me, maybe someone already encountered similar problem and solved it in simple way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尚未指定传递回调上的返回类型以替换,并且如书面而没有提供返回值,因此可以推断出未定义。指定返回类型,或确保所有代码路径返回预期类型(字符串)的值
You haven't specified a return type on the callback passed to replace, and as written it doesn't provide a return value so is inferred to be undefined. Either specify the return type, or make sure all code paths return a value of the expected type (string)