realm-js
realm-js copied to clipboard
Mismatch type when passing `user` from `useUser` as parameter
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
@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.
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)
@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 = ...
I'll be taking over this issue and try to reproduce it 👍