Corey Alix
Corey Alix

Reputation: 2750

Typescript "specialized" overloading

Is it possible to define this specialized overload without introducing the signature which exactly matches the implementation?

on(eventName: string, cb: Function);
on(eventName: "view", cb: (args: {
    foo: {
    }
}) => void);
on(eventName: string, cb: Function) {
}

When I remove it I receive this error:

Specialized overload signature is not assignable to any non-specialized signature.

Upvotes: 11

Views: 1599

Answers (1)

Fenton
Fenton

Reputation: 250902

Specialized overloads are a specialism of one of the other overload signatures (the implementation signature is not visible, so doesn't count).

When you use a specialized overload, there must be at least one non-specialized signature that the specialized version "makes special".

The return type of the specialized signature has to be a sub-type of the non-specialized signature.

So in short, you have to have the overload even if it is identical to the implementation signature.

Upvotes: 9

Related Questions