Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong error message when overloading #30843

Closed
ghost opened this issue Apr 10, 2019 · 3 comments
Closed

Wrong error message when overloading #30843

ghost opened this issue Apr 10, 2019 · 3 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@ghost
Copy link

ghost commented Apr 10, 2019

TypeScript Version: 3.4.3

overload error message

Code

class DeviceType {}

class MyClass {
        on(event: 'attach', listener: (device: DeviceType) => void): this;
        on(event: 'detach', listener: (deviceID: number, deviceName: string): this;
        on(event: 'firstScanDone', listener: () => void): this;
        
        on(event: 'attach' | 'detach' | 'firstScanDone',
           listener: ((device: j.DeviceType) => void)
                     | (() => void) 
                     | ((deviceID: number, deviceName: string, productID: string, vendorID: string, ESN: string, isDongleDevice: boolean, isInFirmwareUpdateMode: boolean, variant: string, errorStatus: number) => void)): this {
            // Impl here.
            return this;
        }
}

let test = new MyClass();
test.on('attach', (device: DeviceType, string: number) => {}); // Note error!

Expected behavior:

Sensible error message about callback argument type not matching event or no overload matching signature etc.

Actual behavior:

Error: Argument of type '"attach"' is not assignable to parameter of type '"firstScanDone"'.

Related Issues:

No

@ghost ghost changed the title Wr Wrong error message when overloading Apr 10, 2019
@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Apr 10, 2019
@RyanCavanaugh
Copy link
Member

The error message correctly identifies a possible call to the function. In the presence of an invalid call, we can only ever guess which overload you meant to call; the heuristic is to simply select the last one and report errors against that.

@ghost
Copy link
Author

ghost commented Apr 11, 2019

@RyanCavanaugh The guesswork is extremely misleading in cases like this. Instead of guesswork, the error message should be generic. It would be MUCH better with an error message like this "Invalid call. Can not find appropriate overload."

@RyanCavanaugh
Copy link
Member

It'd be better in this specific case, but extremely worse in many, many other situations. There is a balance to be struck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

1 participant