-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Class method with conditional return type cannot return types common to both conditional branches when the class also has an interface #50822
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
Duplicate #27932 You can write public bar(): number | (T extends string ? string : never) { |
@RyanCavanaugh Thanks for looking into it. I'd be really curious to know why the mere presence of the empty interface triggers the issue, and why the issue does not occur if I comment out the interface. I see the linked issue is an abandoned PR - does that mean this is a wontfix? |
I missed that - no, that's super weird |
Sideways question - what does this construct even mean? Based on my testing this seems like a huge footgun. export interface Foo<T> {
foo(): number
}
export class Foo<T> {
constructor(private _v: T) {}
public bar(): T {
return this._v
}
}
const inst = new Foo(100)
inst.foo() // ok, but crashes at runtime
inst.bar() // ok |
Declaration merging, for declaring when you've e.g. polyfilled |
This happens because a literal
This logic can be found here. In turn, |
Bug Report
π Search Terms
method conditional return type not assignable to type
π Version & Regression Information
3.3.3+
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
Return statement is an error, despite being valid for both branches of the conditional return type.
This ONLY happens if the
interface Foo
exists. If the interface is commented out, the error goes away.π Expected behavior
Return statement is not an error, because
1
is assignable to bothstring | number
andnumber
.The text was updated successfully, but these errors were encountered: