使用替换功能回调时的编译错误时的编译错误

发布于 2025-01-26 20:48:33 字数 1663 浏览 4 评论 0原文

我有一个生成随机单词的函数:

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

从初始搜索开始,我或多或少都知道这已经存在。我看到了其他stackoverflowgit问题与此问题相关的主题,但是我没有找到最简单的方法来解决此问题可以帮助我的问题,也许有人已经遇到了类似的问题和以简单的方式解决了它。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花开柳相依 2025-02-02 20:48:33

您尚未指定传递回调上的返回类型以替换,并且如书面而没有提供返回值,因此可以推断出未定义。指定返回类型,或确保所有代码路径返回预期类型(字符串)的值

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文