Improve TypeScript type definitions in jest.d.ts#203
Conversation
- 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 OverviewGreptile SummaryUpdates documentation links in several package READMEs to point to react.dev. Main functional change is in One correctness issue remains in the spy helper typings: they currently accept keys for non-function properties even though the runtime helpers wrap Confidence Score: 4/5
Important Files Changed
|
| declare function spyOnDev<T extends object, M extends keyof T>( | ||
| obj: T, | ||
| key: M | ||
| ): MockFunction<T[M]>; |
There was a problem hiding this 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.
| 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.| declare function spyOnDevAndProd<T extends object, M extends keyof T>( | ||
| obj: T, | ||
| key: M | ||
| ): MockFunction<T[M]>; |
There was a problem hiding this comment.
logic: Same issue as spyOnDev - MockFunction<T[M]> incorrectly uses the entire property type instead of extracting the return type.
| 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.| declare function spyOnProd<T extends object, M extends keyof T>( | ||
| obj: T, | ||
| key: M | ||
| ): MockFunction<T[M]>; |
There was a problem hiding this comment.
logic: Same issue as other spy functions - MockFunction<T[M]> should not use the property type directly as the generic parameter.
| 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'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". |
| declare function spyOnDev<T extends object, M extends keyof T>( | ||
| obj: T, | ||
| key: M | ||
| ): MockFunction<T[M]>; |
There was a problem hiding this 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.
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.|
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. |
Mirror of facebook/react#35198
Original author: Rezakarimzadeh98
Summary
This PR improves TypeScript type definitions in
scripts/jest/typescript/jest.d.tsto provide better type safety and IntelliSense support for React's internal test suite.Changes
expect()andExpectinterface for better type inferencebeforeEach,afterEach, etc.)spyOnDev,spyOnProd) withkeyofconstraintsObjecttype to lowercaseobject(TypeScript best practice)arguments→argsto avoid reserved keyword)MockFunctioninterfaceBenefits
Testing
Example