Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript "specialized" overloading

Tags:

typescript

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.

like image 231
Corey Alix Avatar asked Aug 25 '15 16:08

Corey Alix


1 Answers

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.

like image 195
Fenton Avatar answered Oct 19 '22 09:10

Fenton