2

I keep getting following error:

error TS2394: Overload signature is not compatible with function implementation.

for following code:

import Registry = require('../class/Registry');

type Format = Function;

type FormatRegistry = {
  [s: string]: Format;
}

var registry = new Registry<Format>();

function formatAPI(): FormatRegistry; // get all formats // HERE GOES THE COMPILER ERROR
function formatAPI(name: string): Format; // get format
function formatAPI(name: string, format: Format): void; // register format
function formatAPI(formatMap: FormatRegistry): void; // register many formats
function formatAPI(nameOrMap?: string|FormatRegistry, callback?: Format): FormatRegistry|Format|void {
  if (typeof nameOrMap === 'object') {
    registry.registerMany(nameOrMap);
  } else if (typeof nameOrMap === 'string') {
    if (callback) {
      registry.register(nameOrMap, callback);
    } else {
      return registry.get(nameOrMap);
    }
  } else {
    return registry.list();
  } 
}

export = formatAPI;

I can't see what is wrong in the function type overloading above. I tried to remove functions signatures (and according types) piece by piece but I failed - I achieved an empty function.

The logic behind the codeis fine (all tests pass), just the overloads are somehow invalid.

This question is related to this one.

Community
  • 1
  • 1
ducin
  • 25,621
  • 41
  • 157
  • 256

1 Answers1

0

This all works without errors for me in the typescript playground:

class Registry<T> {
    registerMany(items: FormatRegistry) { } 
    register(name: string, cb: Format) { }
    get(name: string) { }
    list() { }
}

type Format = Function;

type FormatRegistry = {
  [s: string]: Format;
}

var registry = new Registry<Format>();

function formatAPI(): FormatRegistry; // get all formats // HERE GOES THE COMPILER ERROR
function formatAPI(name: string): Format; // get format
function formatAPI(name: string, format: Format): void; // register format
function formatAPI(formatMap: FormatRegistry): void; // register many formats
function formatAPI(nameOrMap?: string|FormatRegistry, callback?: Format): FormatRegistry|Format|void {
  if (typeof nameOrMap === 'object') {
    registry.registerMany(nameOrMap);
  } else if (typeof nameOrMap === 'string') {
    if (callback) {
      registry.register(nameOrMap, callback);
    } else {
      return registry.get(nameOrMap);
    }
  } else {
    return registry.list();
  } 
}

export = formatAPI;
Corey Alix
  • 2,694
  • 2
  • 27
  • 38
  • My code works for me in typescript playground as well. But the compiler on my computer throws this error... – ducin Apr 15 '16 at 17:16
  • What version of tsc? – Corey Alix Apr 15 '16 at 17:21
  • 1
    typescript "1.8.9" – ducin Apr 15 '16 at 17:33
  • I updated to 1.8.10 and no problems. Share your tsconfig.json maybe. I created a registry.ts and it still worked: `export type Format = Function; export type FormatRegistry = { [s: string]: Format; } export class Registry { registerMany(items: FormatRegistry) { } register(name: string, cb: Format) { } get(name: string) { } list() { } }` – Corey Alix Apr 15 '16 at 18:50