Skip to content
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

Isolated declarations should infer void and Promise<void> for methods without return statements #60993

Open
6 tasks done
rictic opened this issue Jan 17, 2025 · 1 comment
Open
6 tasks done

Comments

@rictic
Copy link
Contributor

rictic commented Jan 17, 2025

πŸ” 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 of Promise<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 just return 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 (or Promise<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:

export function clickHeader() {
    document.querySelector('h1')?.click();
}

export async function loadContent() {
    await import('./extra-content.js');
}

πŸ’» Use Cases

  1. What do you want to use this for?

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.

  1. What shortcomings exist with current approaches? What workarounds are you using?

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.

@jakebailey
Copy link
Member

jakebailey commented Jan 18, 2025

Duplicate of #58330

The problem is that never can also be inferred due to calls to other functions that return never, which can only be understood through type analysis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants