Skip to content

fix: Do not infer generic Props type from RenderHookOptions #1816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2025

Conversation

AlterNT
Copy link
Contributor

@AlterNT AlterNT commented Aug 22, 2025

Summary

Unsure when this happened, but noticed it when bumping a react-native project to 0.80.1. (Using typescript 5.5.4)

renderHook and renderHookAsync should not infer Props from RenderHookOptions.
See the following example:

type ExampleProps = {
  required: string;
  optional?: number;
};

const exampleHook = (_: ExampleProps) => {};

const { rerender } = renderHook(exampleHook, {
  initialProps: {
    required: '',
    unknown: 1,
  },
});

rerender({
  required: '',
  unknown: 1, // This should be invalid
  optional: 1, // <--- type error here despite optional being a valid prop
});

This is resulting in optional props not specified in the initialProps setting to type-error when using rerender().

This is because the Props generic type is being inferred from both ExampleProps and { required: string, unknown: number } due to it appearing also in initialProps.

This is addressed by wrapping it with NoInfer<> so that the options are ignored for inferring Props

Test plan

After the fix is applied, the type-errors are correctly pointing out that unknown is not a prop.

type ExampleProps = {
  required: string;
  optional?: number;
};

const exampleHook = (_: ExampleProps) => {};

const { rerender } = renderHook(exampleHook, {
  initialProps: {
    required: '',
    unknown: 1, // <--- type error here
  },
});

rerender({
  required: '',
  unknown: 1, // <--- type error here
  optional: 1, 
});

Copy link

codecov bot commented Aug 22, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.94%. Comparing base (6ee46e6) to head (ebef2fa).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #1816    +/-   ##
========================================
  Coverage   89.94%   89.94%            
========================================
  Files          98       98            
  Lines        5856     5856            
  Branches      943      605   -338     
========================================
  Hits         5267     5267            
  Misses        584      584            
  Partials        5        5            

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@mdjastrzebski mdjastrzebski left a comment

Choose a reason for hiding this comment

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

LGTM

@mdjastrzebski mdjastrzebski merged commit 919f7c6 into callstack:main Aug 22, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants