Skip to content

Improve TypeScript type definitions in jest.d.ts#203

Open
everettbu wants to merge 12 commits into
mainfrom
main-35198
Open

Improve TypeScript type definitions in jest.d.ts#203
everettbu wants to merge 12 commits into
mainfrom
main-35198

Conversation

@everettbu

@everettbu everettbu commented Dec 12, 2025

Copy link
Copy Markdown

Mirror of facebook/react#35198
Original author: Rezakarimzadeh98


Summary

This PR improves TypeScript type definitions in scripts/jest/typescript/jest.d.ts to provide better type safety and IntelliSense support for React's internal test suite.

Changes

  • Add generic type parameters to expect() and Expect interface for better type inference
  • Add proper function signatures for test lifecycle functions (beforeEach, afterEach, etc.)
  • Add type-safe generic spy functions (spyOnDev, spyOnProd) with keyof constraints
  • Convert deprecated Object type to lowercase object (TypeScript best practice)
  • Fix parameter names (argumentsargs to avoid reserved keyword)
  • Add generic type parameter to MockFunction interface

Benefits

  • Better IntelliSense: Autocomplete shows correct types when writing tests
  • Catch errors early: TypeScript catches type mismatches at compile time
  • Modern TypeScript: Uses current best practices and conventions
  • Zero runtime impact: Pure compile-time improvement with no breaking changes

Testing

  • ✅ ESLint passes with no errors
  • ✅ TypeScript compilation successful
  • ✅ No breaking changes - fully backward compatible

Example

// Before: No type safety
const spy = spyOnDev(MyObject, 'myMethod'); // Returns 'any'

// After: Full type safety
const spy = spyOnDev(MyObject, 'myMethod'); // Fully typed with autocomplete
//                            ^^^^^^^^ TypeScript ensures this key exists

- Add generic type parameters to expect() and Expect interface
- Add proper function signatures for test lifecycle functions
- Add type-safe generic spy functions with keyof constraints
- Convert Object to lowercase object (TypeScript best practice)
- Fix parameter names (arguments -> args)
- Add generic type parameter to MockFunction interface

These improvements provide better type safety and IntelliSense
support for React's internal test suite with zero runtime impact.
- Update react package README production build link
- Update useSyncExternalStore documentation link
- Update react-test-renderer documentation link
- Update react-devtools-inline release channel link
- Update react-devtools React website link

All links now point to the current react.dev documentation
instead of the deprecated reactjs.org domain.
@greptile-apps

greptile-apps Bot commented Dec 12, 2025

Copy link
Copy Markdown

Greptile Overview

Greptile Summary

Updates documentation links in several package READMEs to point to react.dev.

Main functional change is in scripts/jest/typescript/jest.d.ts, where the ambient Jest typings used by React’s internal test suite are made more specific: lifecycle hooks now accept sync/async callbacks, expect()/Expect gain generics for better inference, and spy helpers are typed with keyof constraints.

One correctness issue remains in the spy helper typings: they currently accept keys for non-function properties even though the runtime helpers wrap jest.spyOn (method-only).

Confidence Score: 4/5

  • Generally safe to merge, with one type-correctness issue in the new spy helper typings.
  • Most changes are documentation-link updates and type-definition tightening. The remaining issue is localized to jest.d.ts where spy helpers currently allow non-function keys, which can mislead TS users and diverges from runtime jest.spyOn behavior.
  • scripts/jest/typescript/jest.d.ts

Important Files Changed

Filename Overview
scripts/jest/typescript/jest.d.ts Tightens Jest ambient typings (expect generics, lifecycle function signatures, spy helpers), but spy helpers still accept non-function keys which mismatches runtime jest.spyOn expectations.
packages/react-devtools-inline/README.md Updates experimental release-channel documentation link to react.dev.
packages/react-devtools/README.md Updates React website link from reactjs.org to react.dev.
packages/react-test-renderer/README.md Updates documentation link to react.dev reference page.
packages/react/README.md Updates production build documentation link to react.dev.
packages/use-sync-external-store/README.md Updates hook reference link to react.dev.

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

6 files reviewed, no comments

Edit Code Review Agent Settings | Greptile

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

6 files reviewed, 3 comments

Edit Code Review Agent Settings | Greptile

Comment on lines +15 to +18
declare function spyOnDev<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

logic: Return type MockFunction<T[M]> assumes T[M] is the property type, but should represent the function's return type. For methods like console.log, T[M] is the entire function type (...args) => void, not just void.

Suggested change
declare function spyOnDev<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;
declare function spyOnDev<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction;
Prompt To Fix With AI
This is a comment left during a code review.
Path: scripts/jest/typescript/jest.d.ts
Line: 15:18

Comment:
**logic:** Return type `MockFunction<T[M]>` assumes `T[M]` is the property type, but should represent the function's return type. For methods like `console.log`, `T[M]` is the entire function type `(...args) => void`, not just `void`.

```suggestion
declare function spyOnDev<T extends object, M extends keyof T>(
  obj: T,
  key: M
): MockFunction;
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +19 to +22
declare function spyOnDevAndProd<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

logic: Same issue as spyOnDev - MockFunction<T[M]> incorrectly uses the entire property type instead of extracting the return type.

Suggested change
declare function spyOnDevAndProd<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;
declare function spyOnDevAndProd<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction;
Prompt To Fix With AI
This is a comment left during a code review.
Path: scripts/jest/typescript/jest.d.ts
Line: 19:22

Comment:
**logic:** Same issue as `spyOnDev` - `MockFunction<T[M]>` incorrectly uses the entire property type instead of extracting the return type.

```suggestion
declare function spyOnDevAndProd<T extends object, M extends keyof T>(
  obj: T,
  key: M
): MockFunction;
```

How can I resolve this? If you propose a fix, please make it concise.

Comment on lines +23 to +26
declare function spyOnProd<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

logic: Same issue as other spy functions - MockFunction<T[M]> should not use the property type directly as the generic parameter.

Suggested change
declare function spyOnProd<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;
declare function spyOnProd<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction;
Prompt To Fix With AI
This is a comment left during a code review.
Path: scripts/jest/typescript/jest.d.ts
Line: 23:26

Comment:
**logic:** Same issue as other spy functions - `MockFunction<T[M]>` should not use the property type directly as the generic parameter.

```suggestion
declare function spyOnProd<T extends object, M extends keyof T>(
  obj: T,
  key: M
): MockFunction;
```

How can I resolve this? If you propose a fix, please make it concise.

@greptile-apps

greptile-apps Bot commented Dec 20, 2025

Copy link
Copy Markdown

Greptile's behavior is changing!

From now on, if a review finishes with no comments, we will not post an additional "statistics" comment to confirm that our review found nothing to comment on. However, you can confirm that we reviewed your changes in the status check section.

This feature can be toggled off in your Code Review Settings by deselecting "Create a status check for each PR".

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

6 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

Comment on lines +15 to +18
declare function spyOnDev<T extends object, M extends keyof T>(
obj: T,
key: M
): MockFunction<T[M]>;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Spy keys not functions

spyOnDev/spyOnProd/spyOnDevAndProd currently allow key: keyof T with no constraint that T[key] is callable. At runtime these are wrappers around jest.spyOn (scripts/jest/setupTests.js:18-46 and scripts/jest/spec-equivalence-reporter/setupTests.js:15-42), which expects a method/function property; spying on non-functions is invalid and the typings will incorrectly accept e.g. spyOnDev({count: 1}, 'count') and return a callable MockFunction. Consider constraining M to function-valued keys (e.g. M extends keyof T & (T[M] extends (...args:any)=>any ? M : never) pattern, or a helper type) so invalid keys become a type error.

Prompt To Fix With AI
This is a comment left during a code review.
Path: scripts/jest/typescript/jest.d.ts
Line: 15:18

Comment:
**Spy keys not functions**

`spyOnDev/spyOnProd/spyOnDevAndProd` currently allow `key: keyof T` with no constraint that `T[key]` is callable. At runtime these are wrappers around `jest.spyOn` (`scripts/jest/setupTests.js:18-46` and `scripts/jest/spec-equivalence-reporter/setupTests.js:15-42`), which expects a method/function property; spying on non-functions is invalid and the typings will incorrectly accept e.g. `spyOnDev({count: 1}, 'count')` and return a callable `MockFunction`. Consider constraining `M` to function-valued keys (e.g. `M extends keyof T & (T[M] extends (...args:any)=>any ? M : never)` pattern, or a helper type) so invalid keys become a type error.

How can I resolve this? If you propose a fix, please make it concise.

@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated.

@github-actions github-actions Bot added the Resolution: Stale Automatically closed due to inactivity label May 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Resolution: Stale Automatically closed due to inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants