Function return type inferrence expecting () => undefined
may refine impl to () => void
which breaks
#49400
Labels
Duplicate
An existing issue was already created
Bug Report
When defining interfaces with methods returning
undefined
, it would help if TypeScript could properly infer that the implementation does returnundefined
and not justvoid
.Maybe related: TS2355
A function whose declared type is neither 'void' nor 'any' must return a value.
should include return types includingundefined
in its exception set for functions that don't require an explicit return statement.🔎 Search Terms
undefined function void
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The
impl
assignment fails because it evaluatesimpl.method1
as() => Promise<void>
andimpl.method2
as() => void
.For the snippet to work we have to write unnecessary boilerplate:
🙂 Expected behavior
I would expect TypeScript to type
impl.method1
as() => Promise<undefined>
andimpl.method2
as() => undefined
based on the expected type ofimpl
being defined and the fact that the implementations do in fact returnundefined
written as is.Moreover, we get error 2355 in the class
Impl
sayingA function whose declared type is neither 'void' nor 'any' must return a value.
I would argue that TS could expand the explicit return value exception to return types including
undefined
.The text was updated successfully, but these errors were encountered: