Use Awaited type to avoid Promise<Promise<T>>#68
Use Awaited type to avoid Promise<Promise<T>>#68chodorowicz merged 7 commits intochodorowicz:mainfrom
Conversation
|
Hey @urbanfly The change makes sense, thanks. There's one issue though, the type for the callback functions if you're still around and interested in merging this change, I'm happy to do it after this issue is fixed. |
|
I didn't originally get your point about the callback/options. I have removed |
|
Thanks, looks good! There's conflict with the base branch, which needs to get resolved first (both merge and rebase are fine)
Happy to merge this after resolving this two issue. |
d17879f to
efc2d7c
Compare
|
I've rebased and tried to run the tests. The Jest tests pass, but
It seems like it depends on the
I was able to work-around it locally by removing the |
There was a problem hiding this comment.
Pull request overview
This PR improves the TypeScript typings for debounce so that debouncing a Promise-returning function yields Promise<T> instead of Promise<Promise<T>>, leveraging TypeScript 4.5’s Awaited<T> utility type.
Changes:
- Update
DebouncedFunction/internal Promise generics to useAwaited<ReturnType<F>>for the debounced return type. - Add a
tsdassertion to ensure the debounced function type is assignable to() => Promise<number>for async functions. - Bump tooling dependencies (TypeScript, Jest, ts-jest, microbundle, tsd) to versions compatible with
Awaited.
Reviewed changes
Copilot reviewed 3 out of 5 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/index.ts | Uses Awaited<ReturnType<F>> to prevent Promise<Promise<T>> in debounced return types. |
| src/index.test-d.ts | Adds type-level assertions verifying the debounced return type is a single Promise. |
| package.json | Updates dev tooling versions to support Awaited and related build/test tooling. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
The CI job, which includes The last thing is to remove |
urbanfly
left a comment
There was a problem hiding this comment.
I don't know how I "Started a review". I just replied to Copilots's comment re: @types/jest version within VS Code.
Updated Jest doesn't accept "modern" anymore. See https://jest-archive-august-2023.netlify.app/docs/28.x/upgrading-to-jest28/#jestusefaketimers
|
I saw the build failure and have now properly installed Updating to Jest v29 made |
|
@all-contributors add @urbanfly for code |
|
I've put up a pull request to add @urbanfly! 🎉 |
|
Thanks for your contribution @urbanfly 🙇 |

When debouncing a Promise-returning function, the returned function has a type of
Promise<Promise<T>>which is not accurate - see microsoft/TypeScript#27711In TypeScript 4.5, a new
Awaited<T>type was introduced to solve this problem.This PR simply wraps the
ReturnType<F>withAwaited<...>in order to get an accurate return type for the created function.Several packages were updated: