Add TypeScript declarations for requestIdleCallback and cancelIdleCallback#57545
Add TypeScript declarations for requestIdleCallback and cancelIdleCallback#57545Critteros wants to merge 2 commits into
Conversation
…lback requestIdleCallback and cancelIdleCallback exist at runtime on both architectures and are declared in the Flow libdefs, but the public TypeScript types never included them. React Native projects compile without lib: ["dom"], so the API was invisible to TypeScript users - Added the declarations to src/types/globals.d.ts - Add type tests in __typetests__/globals.tsx
|
Hi @Critteros! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
| function cancelAnimationFrame(handle: number | null | undefined): void; | ||
| function requestAnimationFrame(callback: (time: number) => void): number; | ||
|
|
||
| type IdleCallbackID = unknown; |
There was a problem hiding this comment.
I think we should just type this as a number, as define in the spec. In Flow we use an opaque type here because it's a better way to model it, but TS doesn't support them. Using unknown would allow more types into cancelIdleCallback than necessary, so restricting them to number is slightly safer.
There was a problem hiding this comment.
good call on that, I've changed the definition to use number instead of unknown
Summary:
requestIdleCallbackandcancelIdleCallbackexist at runtime on both architectures. The New Architecture has a native implementation since 0.75 in #44759 (landed as abfadc6, closing #44636). Legacy architecture has a JS-timer-based implementation inJSTimerssince 2016 (18394fb, follow-up to #5052). Support for theoptionsparam was added in cf51aee. The API is documented at https://reactnative.dev/docs/global-requestIdleCallbackFlow libdefs already declare both functions in
flow/global.jsThe public TypeScript declarations never included them.
React Native projects compile without
lib: ["dom"], so TypeScript users get no declarationThis PR adds the declarations to the
Timer Functionsregion ofsrc/types/globals.d.ts, based on the Flow signaturesChangelog:
[GENERAL] [ADDED] - Add TypeScript declarations for
requestIdleCallbackandcancelIdleCallbackTest Plan:
Added
testRequestIdleCallback()topackages/react-native/__typetests__/globals.tsx