microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.48k stars 12.43k forks source link

Implementation signatures that do not contain all overloaded signatures are allowed in class definitions #55963

Open sw6ueyz opened 1 year ago

sw6ueyz commented 1 year ago

🔎 Search Terms

implementation contain overloaded class member

🕗 Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.2.2#code/FAYwNghgzlAEDCwDexZtgMwPZYBSwgH5YAuWAOwFcBbAIwFMAnWASlNgDcsBLAEwG5U6bHgLsqdJgBpYtcTQbM2ZLn0HpMOfBHmTGM2sTITFrdqt5C0KDRpBZyULGHoA6MFgDm+AC4BPAAd6LAwxFn5YAHpI2H8gkLFuOBMmK1gAX2BM4HJ6AHcEXBZXESKIqJiAxm5yHzgAIkpyXnoMGvpeeuAgA

💻 Code

class C
{
    foo( a? : number ) : void;
    foo( a : number, b : number ) : void;
    foo( a : number, b? : number ) : void
    {
        console.log( typeof a ); // typeof a is number
    }
}

new C().foo();  // prints "undefined"

🙁 Actual behavior

The compilation succeeded.

The type of argument "a" in function foo is number but prints "undefined" which is wrong.

Because the implementation signature of foo does not include all overload signatures, so compilation should fail.

🙂 Expected behavior

The compilation should fail.

Additional information about the issue

This bug is only reproduced with class member functions.

RyanCavanaugh commented 1 year ago

I agree this is a bug. When comparing the overload implementation to the overload list, we shouldn't be taking method bivariance into account.

Odds seem pretty high that this is going to cause a huge amount of unnecessary breaks and we'll end up rejecting the fix 🫠