Home > Blockchain >  compilation error at overload when using replacerFunction callback
compilation error at overload when using replacerFunction callback

Time:05-08

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.

CodePudding user response:

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)

  • Related