Reputation: 19762
What am I doing wrong here:
export function fail<a>(problem: SomeProblem): a;
export function fail<a>(message: string): a;
export function fail<a>(messageOrProblem: any): a { throw Error(); }
the compiler says:
TS2148: Build: Overload signature is not compatible with function definition.
Upvotes: 1
Views: 2182
Reputation: 221392
The type parameters here are considered 'different' because they come from different places. You should write this like so:
export function fail<a>(problem: SomeProblem): a;
export function fail<a>(message: string): a;
export function fail(messageOrProblem: any): any { throw Error(); }
As an aside, using a generic type argument only in a return value position is sort of an anti-pattern. Since you have no way to determine what value to return based on a
, it's much more accurate to return any
than to return an uninferable generic type. I call this the "moving the cast" pattern:
// Bad
x = fail<string>('foo'); // This looks more typesafe than it is
// Good
x = <string>fail('foo'); // Let's be honest with ourselves
Upvotes: 6