-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Cannot overload interface method with different return types #54354
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
Comments
Essentially #47669 (focuses on arrow functions but it’s the same issue: we currently have no way to say “check this the same loose way that overload implementation are checked”) |
These are just correct errors; the function provided doesn't match the overloads you wrote and a valid call could result in an unsound result. |
I mean to be fair that's true of regular overloads too, and |
Hmm, but how doesn't it match? Also, why do the inline overflows work then? |
function createShape(): Shape {
return {
// ❌
area(_x: number, _y?: number): number | string {
return 0;
},
};
}
createShape().area(3, 3).substring(0); // no error, but crashes I can't figure out what you're proposing. Are you saying that any overload implementation signature should be an error too? |
Basically this: interface Shape {
area(x: number): number;
area(x: number, y: number): string;
}
function createShape(): Shape {
return {
area(_x: number, _y?: number): number | string {
return 0;
},
};
}
const num: number = createShape().area(3);
const str: string = createShape().area(3, 3); |
People would complain if we did that because it would be inconsistent, since this program would have to error: interface Shape {
area(x: number): number;
area(x: number, y: number): string;
}
function createShape(): Shape {
const x = {
area(_x: number, _y?: number): number | string {
return 0;
},
};
return x;
}
const num: number = createShape().area(3);
const str: string = createShape().area(3, 3); |
Ok, but, whats the difference here? // ❌ doesn't work
interface Shape {
area(x: number): number;
area(x: number, y: number): string;
}
class ShapeImpl implements Shape {
area(_x: number, _y?: number): number | string {
return 0;
}
}
const num: number = (new ShapeImpl()).area(3);
const str: string = (new ShapeImpl()).area(3, 3); // ✅ works
class ShapeImpl {
area(x: number): number;
area(x: number, y: number): string;
area(_x: number, _y?: number): number | string {
return 0;
}
}
const num: number = (new ShapeImpl()).area(3);
const str: string = (new ShapeImpl()).area(3, 3); |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@RyanCavanaugh I don't want to be a nuisance, I just want to understand - can you please explain #54354 (comment)? Thanks in advance! |
I don't understand the question. You're basically saying that if you an |
@enisdenjo I think I understand what Ryan is getting at - currently, @RyanCavanaugh The above being said, I don’t think that’s necessarily true - I’m envisioning that if you call the method directly through a |
@RyanCavanaugh no, I am saying they should. This also doesn't work: // ❌ doesn't work
interface Shape {
area(x: number): number;
area(x: number, y: number): string;
area(x: number, y?: number): number | string;
}
class ShapeImpl implements Shape {
area(_x: number, _y?: number): number | string {
return 0;
}
}
const num: number = (new ShapeImpl()).area(3);
const str: string = (new ShapeImpl()).area(3, 3); Why are overloads different when defined in interfaces compared to directly on functions/methods? |
class ShapeImpl implements Shape {
area(_x: number, _y?: number): number | string {
return 0;
}
} and class ShapeImpl {
area(_x: number, _y?: number): number | string {
return 0;
}
}
|
Uh, but then what? As soon as you write const x: Shape = new ShapeImpl(); you'll get a type error, which is the exact error you were trying to ensure wouldn't happen by writing |
@RyanCavanaugh I’d probably just expect that assignment to succeed. |
Are you just saying that any signature which roughly matches an overload list should be assignable to it? Or that classes with |
Bug Report
Defining an overloaded method in an interface raises an error during implementation if overloads have different return types.
🔎 Search Terms
overload
,method
,interface
,factory function
🕗 Version & Regression Information
Doesn't work in v3, v4 or v5.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
🙂 Expected behavior
More details
If the return type does not change, overloads work as expected.
✅
Also works if the overloads are defined in the class or on root level functions
✅
The text was updated successfully, but these errors were encountered: