typescript function_error ai_generated true

TS2394: This overload signature is not compatible with its implementation signature

ID: typescript/ts-overload-signature-error

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
5 active

Root Cause

The implementation signature must be compatible with all overload signatures. It must accept a superset of all parameter types.

generic

Workarounds

  1. 92% success Ensure the implementation signature accepts all overload parameter types
    function parse(input: string): Document;
    function parse(input: Buffer): Document;
    function parse(input: string | Buffer): Document {
      // implementation handles both types
      const text = typeof input === 'string' ? input : input.toString();
      return parseDocument(text);
    }

    Sources: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads

  2. 85% success Use a union type in the implementation for return types
    function getValue(key: 'name'): string;
    function getValue(key: 'age'): number;
    function getValue(key: 'name' | 'age'): string | number {
      // implementation return type is union of all overload returns
    }

    Sources: https://www.typescriptlang.org/docs/handbook/2/functions.html#overload-signatures-and-the-implementation-signature

Dead Ends

Common approaches that don't work:

  1. Make each overload signature identical to the implementation 65% fail

    Defeats the purpose of overloads; overloads should provide narrower type signatures for callers

Error Chain

Leads to:
Preceded by:
Frequently confused with: