Skip to content
New issue

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

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

Already on GitHub? # to your account

Mismatch type when passing user from useUser as parameter #6196

Open
maichongju opened this issue Oct 16, 2023 · 4 comments · Fixed by #6206
Open

Mismatch type when passing user from useUser as parameter #6196

maichongju opened this issue Oct 16, 2023 · 4 comments · Fixed by #6206

Comments

@maichongju
Copy link

maichongju commented Oct 16, 2023

How frequently does the bug occur?

Always

Description

As the documentation suggested, we can pass the user to a function.

// Gets a valid user access token to authenticate requests
async function getValidAccessToken(user: Realm.User) {
// An already logged in user's access token might be stale. To
// guarantee that the token is valid, refresh it if necessary.
await user.refreshCustomData();
 
return user.accessToken;
}

However, when passing the user from useUser(). following error display.

Argument of type
User<DefaultFunctionsFactory, Record<string, unknown>, DefaultUserProfileData>
is not assignable to parameter of type
UserType<UserFunctionsType, UserCustomDataType, UserProfileDataType>
Types have separate declarations of a private property  cachedProfile 

Stacktrace & log output

No response

Can you reproduce the bug?

Always

Reproduction Steps

file1.ts

import Realm from 'realm';

export function foo(user: Realm.User){
  // Do something
}

file2.ts

import {foo} from file1;

const user = useUser();
foo(user);

Version

"realm": "^12.2.1",, "@realm/react": "^0.6.0",

What services are you using?

Atlas App Services: Functions or GraphQL or DataAPI etc

Are you using encryption?

No

Platform OS and version(s)

Win11 23H2

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

@takameyer
Copy link
Contributor

takameyer commented Oct 20, 2023

@maichongju Thanks for reporting! It seems using rollup for generated type declarations was causing some trouble. I opened a PR which uses good old tsc to do this and the results have seemed to improve. Most importantly, it fixes the issue you have. Once this has been reviewed and merged we will cut a release.

@CarlosVilasAlvarez
Copy link

Using realm 12.3.1 and @realm/react 0.6.2 (the version when this should have been fixed) the error still persists when I use Realm.User as a type for function parameter and the value from useUser().

Argument of type 'User<DefaultFunctionsFactory, Record<string, unknown>, DefaultUserProfileData>' is not assignable to parameter of type 'UserType<UserFunctionsType, UserCustomDataType, UserProfileDataType>'.
Type 'DefaultFunctionsFactory' is not assignable to type 'UserFunctionsType'.
'DefaultFunctionsFactory' is assignable to the constraint of type 'UserFunctionsType', but 'UserFunctionsType' could be instantiated with a different subtype of constraint 'DefaultFunctionsFactory'.ts(2345)

@CarlosVilasAlvarez
Copy link

@takameyer Okay, it seems that this happens when I use the following import

import { User } from 'realm'

but it works when I use the following type

import Realm from 'realm
 
const user: Realm.User = ...

@kraenhansen kraenhansen assigned kraenhansen and unassigned takameyer Feb 14, 2024
@kraenhansen kraenhansen reopened this Feb 14, 2024
@kraenhansen
Copy link
Member

I'll be taking over this issue and try to reproduce it 👍

@sync-by-unito sync-by-unito bot changed the title Mismatch type when passing user from useUser as parameter Mismatch type when passing user from useUser as parameter Feb 20, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants