Skip to content

Add TypeScript declarations for requestIdleCallback and cancelIdleCallback#57545

Open
Critteros wants to merge 2 commits into
react:mainfrom
Critteros:typings-for-request-idle-callback
Open

Add TypeScript declarations for requestIdleCallback and cancelIdleCallback#57545
Critteros wants to merge 2 commits into
react:mainfrom
Critteros:typings-for-request-idle-callback

Conversation

@Critteros

Copy link
Copy Markdown

Summary:

requestIdleCallback and cancelIdleCallback exist 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 in JSTimers since 2016 (18394fb, follow-up to #5052). Support for the options param was added in cf51aee. The API is documented at https://reactnative.dev/docs/global-requestIdleCallback

Flow libdefs already declare both functions inflow/global.js

The public TypeScript declarations never included them.
React Native projects compile without lib: ["dom"], so TypeScript users get no declaration

This PR adds the declarations to the Timer Functions region of src/types/globals.d.ts, based on the Flow signatures

Changelog:

[GENERAL] [ADDED] - Add TypeScript declarations for requestIdleCallback and cancelIdleCallback

Test Plan:

Added testRequestIdleCallback() to packages/react-native/__typetests__/globals.tsx

yarn test-generated-typescript
yarn test-typescript-legacy
yarn lint
yarn prettier

…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
@meta-cla

meta-cla Bot commented Jul 14, 2026

Copy link
Copy Markdown

Hi @Critteros!

Thank you for your pull request and welcome to our community.

Action Required

In 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.

Process

In 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 CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Jul 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 14, 2026
@meta-cla

meta-cla Bot commented Jul 14, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 14, 2026
function cancelAnimationFrame(handle: number | null | undefined): void;
function requestAnimationFrame(callback: (time: number) => void): number;

type IdleCallbackID = unknown;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call on that, I've changed the definition to use number instead of unknown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants