1

I'm trying to override the a function signature defined in a TypeScript definition file, however I can't quite get it working. Below is the code that I have, but Typescript seems to be ignoring the augmentation, and is erroring saying that the function expects 2 arguments, but has been given 3.

If I remove the function definition inside the definition file it does set it correctly, so I'm wondering if there is an issue with specifically overriding an existing definition.

The code:

// package/index.d.ts
export interface RealtimeValueGetter {

    /**
     * Gets the string value of the value, given the payload body
     * @param {Context} ctx The project context.
     * @param {TPayload} payload This instance of the value's payload data.
     */
    getValue: (ctx: Context, payload: unknown) => Promise<string>;
}

export interface EditableRealtimeValue extends RealtimeValueGetter {

    /**
     * Details how Beak and user's should interact with the value editor for your realtime value.
     */
    editor: Editor<TPayload, TEditorState>;
}

// project/augmentations.d.ts
import type { Context, RealtimeValueGetter as OriginalRealtimeValueGetter } from '@getbeak/types/values';

declare module '@getbeak/types-realtime-value' {
    interface RealtimeValueGetter extends Omit<OriginalRealtimeValueGetter, 'getValue'> {

        /**
         * Gets the string value of the value, given the payload body
         * @param {Context} ctx The project context.
         * @param {TPayload} payload This instance of the value's payload data.
         * @param {number} recursiveDepth Used for recursive checks.
         */
        getValue: (ctx: Context, payload: unknown, recursiveDepth: number) => Promise<string>;
    }
}

// project/index.ts
export async function rtvGetValue(ctx: Context, payload: unknown) {
    // Some code removed as unneeded

    // Expected 2 arguments, but got 3. ts(2554)
    return await extension.getValue(context, payload, recursiveDepth);
}

VSCode showing that TypeScript is detecting the second definition: enter image description here

Alexander Forbes-Reed
  • 2,823
  • 4
  • 27
  • 44
  • I think its because of not exporting `RealtimeValueGetter` interface ? – bogdanoff Aug 02 '22 at 14:05
  • Sadly not. It's used internally so I believe you don't need to export it. Also as I mentioned if I remove the definition in the package it does work, even if not exported. – Alexander Forbes-Reed Aug 02 '22 at 14:47
  • [Please replace/supplement images of code/errors with plaintext versions.](https://meta.stackoverflow.com/a/285557/2887218) – jcalz Aug 02 '22 at 16:44
  • @jcalz The error is already included twice as plaintext, once in the first paragraph, and once as a comment in the code snippet above the offending line. The reason for the image is to show that VSCode and the TS language service can detect that both definitions, which is detailed above the image. – Alexander Forbes-Reed Aug 03 '22 at 09:16

0 Answers0