Skip to content
TS2393

    ↑↓ move · ⏎ open · esc close

    TS2393

    Duplicate function implementation

    Duplicate function implementation.

    The compiler’s own words. Not translated — this is the string you pasted into a search box.

    Two function bodies share one name in the same scope; overloads may have many signatures but only one implementation.

    Reproduction

    function parse(value: string) {
      return value
    }
    
    function parse(value: number) {
      return value
    }

    The build asserts this emits exactly this code.

    Why the compiler says this

    JavaScript does not dispatch between same-named function declarations by parameter type; the later declaration would replace the earlier one. TypeScript overloads therefore separate several type-only signatures from one emitted body.

    Fixes

    1. 01
      function parse(value: string): string
      function parse(value: number): number
      function parse(value: string | number): string | number {
        return value
      }

      Write overload signatures followed by one implementation broad enough to handle them all.

    2. 02
      function parseText(value: string) {
        return value
      }
      
      function parseNumber(value: number) {
        return value
      }

      Give different operations different names when they do not need one overloaded API.

    Takeaway

    An overload set has many declarations but exactly one body.

    Where to go next

    Errors