Isolated declarations should infer void
and Promise<void>
for methods without return statements
#60993
Open
6 tasks done
π Search Terms
isolated declaration
isolated declarations
void
Promise
infer return type
β Viability Checklist
β Suggestion
TypeScript infers a return type of
void
for a sync function with no return statements, and a type ofPromise<void>
for an async function with no return statements.This behavior is compatible within the constraints of isolated declarations since it's knowable just from the local AST. This behavior is similar to inferring a return type of
number
for a function that's justreturn 10;
as isolated declarations does today.Simple playground example of code that fails with IsolatedDeclarations today, that I'm proposing be allowed: playground link
π Motivating Example
Functions without return statements have an inferred type of
void
(orPromise<void>
if async). To reduce the amount of friction for adopting and using--isolatedDeclarations
, TypeScript will no longer require an explicit type annotation in cases like these when--isolatedDeclarations
is enabled:π» Use Cases
In adopting isolated declarations in Google's internal codebase, we've found some modules make frequent use of void functions. This will reduce the friction to adopting
--isolatedDeclarations
and in writing and maintaining code using it.We add explicit return types in these cases. Not the worst thing in the world, but nice to reduce friction for trivial cases like this.
The text was updated successfully, but these errors were encountered: