Skip to content
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

Bug: typescript can't determine well useMemo value #28185

Closed
ballonura opened this issue Feb 1, 2024 · 6 comments
Closed

Bug: typescript can't determine well useMemo value #28185

ballonura opened this issue Feb 1, 2024 · 6 comments
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@ballonura
Copy link

ballonura commented Feb 1, 2024

React version: 18.2.0
TypeScript version: 5.0.2

The expected behavior

  const shouldNotRender = useMemo(() => !organization || !ehrUser, [organization, ehrUser]);
  // if (!organization || !ehrUser) return null;

  if (shouldNotRender) return null;

  return (
    <UserManagement
      showNotification={showUserManagementNotification}
      user={{ ...ehrUser, organization }}
      onRolesChanged={onRolesChanged}
    />
  );

Error

Screenshot 2024-02-01 at 14 42 59

The current behavior

  // const shouldNotRender = useMemo(() => !organization || !ehrUser, [organization, ehrUser]);
  if (!organization || !ehrUser) return null;

  // if (shouldNotRender) return null;

  return (
    <UserManagement
      showNotification={showUserManagementNotification}
      user={{ ...ehrUser, organization }}
      onRolesChanged={onRolesChanged}
    />
  );

No error

Screenshot 2024-02-01 at 14 43 32
@ballonura ballonura added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Feb 1, 2024
@eps1lon
Copy link
Collaborator

eps1lon commented Feb 1, 2024

This is a TypeScript limitation I believe. TS would also not refine the type if you put the condition in an function call which is was useMemo basically is.

@ballonura
Copy link
Author

ballonura commented Feb 2, 2024

Should be fixed on TypeScript version 5.4

Preserved Narrowing in Closures Following Last Assignments
YouTube

@HermanBide
Copy link

are you still experiencing the same issues?

@yhekim
Copy link

yhekim commented Feb 25, 2024

The error occurs because TypeScript indicates that the organization variable can be of type Organization or undefined. This is inferred from its usage within the useMemo hook, where it is listed as a dependency along with ehrUser.

When you try to pass organization directly as part of the user object, TypeScript raises an error because organization might be undefined, and therefore cannot be directly assigned to a variable of type Organization.

To resolve this error, you need to check if organization is defined before attempting to use it. You can modify your code to conditionally render the UserManagement component only if organization is defined. For example:

if (shouldNotRender || !organization) return null;

return (
<UserManagement
showNotification={showUserManagementNotification}
user={{ ...ehrUser, organization }}
onRolesChanged={onRolesChanged}
/>
);
In this way, if the organization is undefined, UserManagement will not work and in this way the application will not show errors.

Copy link

This issue has been automatically marked as stale. If this issue is still affecting you, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize it yet. If you have any new additional information, please include it with your comment!

@github-actions github-actions bot added the Resolution: Stale Automatically closed due to inactivity label May 25, 2024
@josephsavona
Copy link
Contributor

Thanks for posting and thanks everyone for suggestions on how to resolve. Since this isn’t an issue with React I’m going to close.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Stale Automatically closed due to inactivity Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

5 participants