-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Incorrect type-error with interface and throws #51102
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
For now, I am using this ugly workaround: foo(): ReturnType<SomeInterface["foo"]> {
throw "Should not be called";
} |
A function consisting only of a
then |
But surely the function, being inherited from an interface, should preserve the inherited type. I'm not saying you should give the function return type never, I'm suggesting that the body is inferred to have type never, and the function keeps its inherited type, which should type check because never is a subtype of anything. |
Method argument and return types are not inferred through inheritance. See #32082 for that. |
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
It's not legal to return a value from a function that's supposed to be of typed |
@RyanCavanaugh I think they meant the inherited method should be inferred as returning |
Ah, right. Of course, that just creates a different problem: interface Foo { bar(): string | undefined }
class MyFoo() implements Foo {
bar() {
return "hello, world";
}
}
const m = new MyFoo();
// m.bar() is possibly undefined -- no it isn't??
const s: string = m.bar(); |
I don't see the problem in your last comment. SomeInterface.foo: string | undefined Thus it should all type check. |
No, it isn't. Because |
Bug Report
π Search Terms
interface, throw
π Version & Regression Information
My version is 4.8.2
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
There is no way to type-check the code above. The return type is inferred as void (I expected
never
). Adding the out-commented return gives the correct return type but errors with "unreachable code." Putting the return type: never
explicitly means the inheriting method doesn't compile.π Expected behavior
Body that throws is inferred to have type
never
andnever <: any
so there should be no errors.The text was updated successfully, but these errors were encountered: