Skip to content

fix: use Promise<Response> for async kit handler return types#2966

Merged
jasonlyu123 merged 5 commits intosveltejs:masterfrom
MathiasWP:fix-async-kit-return-type
Mar 7, 2026
Merged

fix: use Promise<Response> for async kit handler return types#2966
jasonlyu123 merged 5 commits intosveltejs:masterfrom
MathiasWP:fix-async-kit-return-type

Conversation

@MathiasWP
Copy link
Contributor

Summary

When svelte-check adds type annotations to SvelteKit route handlers (GET, POST, PUT, PATCH, DELETE, etc.), it uses Response | Promise<Response> as the return type for all functions. However, TypeScript requires async functions to return Promise<T>, not T | Promise<T> (TS1064).

This caused false positive errors in --incremental mode, where tsc runs directly on the generated overlay files. In non-incremental mode these errors were silently filtered out by the TypeScript plugin's inGenerated diagnostic filtering, but in incremental mode tsc reports them directly.

The bug

// Original code
export async function POST({ request }) { ... }

// Generated overlay (before fix) - triggers TS1064
export async function POST({ request }: RequestEvent) : Response | Promise<Response> { ... }

// Generated overlay (after fix) - correct
export async function POST({ request }: RequestEvent) : Promise<Response> { ... }

Changes

  • packages/svelte2tsx/src/helpers/sveltekit.ts: In addTypeToFunction, check if the function has an async modifier. If so, extract just the Promise<T> part from the return type union instead of using the full T | Promise<T>.
  • packages/svelte2tsx/test/helpers/index.ts: Updated the async GET test to expect Promise<Response> and added a new test for non-async GET to verify it still gets Response | Promise<Response>.
  • packages/svelte-check/test-success/: Added an async +server.ts fixture with GET and POST handlers plus the necessary RequestEvent type to the sanity test suite.

Test plan

  • Updated unit test upserts GET async function fails without fix, passes with fix
  • New unit test upserts GET non-async function passes (non-async still uses Response | Promise<Response>)
  • All 366 svelte2tsx tests pass
  • All 6 svelte-check sanity tests pass (including incremental cold/warm cache)

When svelte-check adds type annotations to SvelteKit route handlers
(GET, POST, etc.), it was using `Response | Promise<Response>` as the
return type for all functions regardless of whether they were async.

TypeScript requires async functions to return `Promise<T>`, not
`T | Promise<T>` (TS1064). This caused false positives in
`--incremental` mode where tsc runs on the generated files directly.

The fix checks if the function has an async modifier and, if so,
extracts just the `Promise<Response>` part of the return type union.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@changeset-bot
Copy link

changeset-bot bot commented Mar 2, 2026

🦋 Changeset detected

Latest commit: f003bca

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
svelte-check Patch
svelte2tsx Patch
svelte-language-server Patch
typescript-svelte-plugin Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

MathiasWP and others added 3 commits March 2, 2026 14:00
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Updated async kit handler return types to use Promise<Response>.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@MathiasWP
Copy link
Contributor Author

We need this fix to be able to use the --incremental flag with svelte-check

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jasonlyu123 jasonlyu123 merged commit 93584d1 into sveltejs:master Mar 7, 2026
3 checks passed
@github-actions github-actions bot mentioned this pull request Mar 7, 2026
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

Successfully merging this pull request may close these issues.

2 participants