The inferred type of 'authClient' cannot be named without a reference...
I was trying to create a shared auth client package in my monorepo, since I have multiple frontends authenticating with the same server..
But I keep getting an error like this (twice) even before adding any plugins...
Literally.. this is my entire code:
I can't disable declaration in my tsconfig, since it's a shared package in my monorepo, and I need the type safety.
I can't add an explicit return type like ReturnType<typeof createAuthClient> since that ruins type safety for any plugins I might add..
Is this a bug in better-auth? Or did I get something wrong with the configuration
9 Replies
I'm getting this error twice:
The inferred type of 'authClient' cannot be named without a reference to '../node_modules/better-auth/dist/shared/better-auth.ClXlabtY.js'. This is likely not portable. A type annotation is necessary.ts(2742)
The inferred type of 'authClient' cannot be named without a reference to '../node_modules/better-auth/dist/shared/better-auth.ZSfSbnQT.js'. This is likely not portable. A type annotation is necessary.ts(2742)
There seems to be a few issues open about this from March, but they're marked as solved, which is really confusing..
I really want to work with better-auth on this project. But this is like the third typescript-related issue I ran into and it's really giving me a headache. Maybe I'm just doing something wrong.
I had similar issues, worked around this by doing things like this:
You just have to break the config down into smaller parts, only the keys would not be type safe but I found that to be fine
And for the plugins, I did
Spread your configs into your betterAuth function in each app that uses it, and you should have decent enough type safety there
This just seems like terrible developer experience 🥲
I'm not sure if I fully understand yet.. but I did something like:
for example, and the error still appears, just now for defaultConfig instead of authClient
Yeah it's not great when it comes to this. If you spread the entire objects you'll get these issues unfortunately. Instead of
satisfies Parameters<typeof createAuthClient>[0]
you should use as const
and type each key if you need to instead e.g.
Something like that...unfortunately that means you lose auto suggestion for config keys but I found that to be manageable. Each key can be copied from the indexed access type i.e. AuthClient["copy-this-here"]I did this, and I still don't get type safety for authClient.admin for example.. Is that what you meant?
Cause then I could just set the return type explicitly to ReturnType<typeof createAuthClient>
Sorry, typing plugins explicitly like that would mean you lose type safety there. Here's an e.g. of my plugins array which is passed to my betterAuth instance.
Above is what you would need to do in each app that uses plugins. I export the default configs from the auth package in my monorepo.
E.g.
man. at this point I might as well just create the client in each app separately and just disable declaration in my tsconfig, since they don't export anything anyway.
@bekacru any chance this will be addressed in a future update? it really sucks to have to either lose my one source of truth, or have to know how to do awesome typescript backflips like @DN_Dev
Yeah that's basically what I had to do, and export as much code as I could to each auth instance in the apps
will be addressed