Why does the same set of overload signatures for the same function work fine when the function is a class method, but not when it's defined as a plain function at module toplevel?
interface Foo {
bar: number;
baz: string;
bat: boolean;
}
function cleanFoo({bar, baz, bat}: Foo): Foo {
return {bar, baz, bat}
}
function cleanFooPartial({bar, baz, bat}: Partial<Foo>): Partial<Foo> {
return {bar, baz, bat}
}
class FooCleaner {
cleanFooOverload(foo: Foo): Foo;
cleanFooOverload(foo: Partial<Foo>): Partial<Foo>;
cleanFooOverload({bar, baz, bat}: Foo): Foo {
return {bar, baz, bat}
}
}
function cleanFooOverload(foo: Foo): Foo;
function cleanFooOverload(foo: Partial<Foo>): Partial<Foo>;
function cleanFooOverload({bar, baz, bat} : Foo): Foo {
return {bar, baz, bat}
}
cleanFoo
is the primary version; cleanFooPartial
is to show that the same implementation matches the overload that takes and returns a Partial<Foo>
; FooCleaner.cleanFooOverload
works fine.
Why does the toplevel definition report this error?
Overload signature is not compatible with function implementation.