Inferred type of betterAuth client exceed compiler serialization

Hi, Im getting this typescript error in my project. Any suggestion on how to solve it? `The inferred type of this node exceeds the maximum length the compiler will serialize. An explicit type annotation is needed.``
17 Replies
discquist
discquistOP4mo ago
Anyone stumbled into the same issue? 🤔
Ping
Ping4mo ago
have you enabled strict in tsconfig? @discquist https://www.better-auth.com/docs/concepts/typescript#strict-mode
TypeScript | Better Auth
Better Auth TypeScript integration.
discquist
discquistOP4mo ago
Yes that's enabled Is there any type i can vast the betterAuth isntance as? That would probably help, but couldn't find.
Big Chunckus
Big Chunckus4mo ago
Same error for me
Ping
Ping4mo ago
Normally this solves it. Can I see your entire tsconfig?
Big Chunckus
Big Chunckus4mo ago
for me it's
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Next.js",
"extends": "./base.json",
"compilerOptions": {
"strict": true,
"plugins": [{ "name": "next" }],
"module": "ESNext",
"moduleResolution": "Bundler",
"allowJs": true,
"jsx": "preserve",
"noEmit": true
}
}
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Next.js",
"extends": "./base.json",
"compilerOptions": {
"strict": true,
"plugins": [{ "name": "next" }],
"module": "ESNext",
"moduleResolution": "Bundler",
"allowJs": true,
"jsx": "preserve",
"noEmit": true
}
}
and base.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"incremental": false,
"isolatedModules": true,
"lib": ["es2022", "DOM", "DOM.Iterable"],
"module": "NodeNext",
"moduleDetection": "force",
"moduleResolution": "NodeNext",
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ES2022"
}
}
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Default",
"compilerOptions": {
"declaration": true,
"declarationMap": true,
"esModuleInterop": true,
"incremental": false,
"isolatedModules": true,
"lib": ["es2022", "DOM", "DOM.Iterable"],
"module": "NodeNext",
"moduleDetection": "force",
"moduleResolution": "NodeNext",
"noUncheckedIndexedAccess": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"target": "ES2022"
}
}
Ping
Ping4mo ago
Check if you disable declaration if it will work
Big Chunckus
Big Chunckus4mo ago
seem to work thank you 🙏
Ping
Ping4mo ago
Hello, @discquist , could you try disabling declaration as well?
discquist
discquistOP4mo ago
Yeah i'll try that.But unsure if i want to do that since i use a ackend framework (encore.ts) that allows me to generate fully typed client to consume in my frontend. @Ping Yeah, not possilbe for me to disbale declaration in my projet. So i htink one solution would be if i can set the type of the instance or something?
Ping
Ping4mo ago
You can set it to ReturnType<typeof betterAuth>, but you won't get any inference for any plugins or other configurations you may have added.
Jakob Norlin
Jakob Norlin3mo ago
I'm also running into this issue. We've got a monorepo with project references, so I can't turn declarations off (because TS requires declarations for project references). It's happening to us when I try to add the stripe plugin. It works fine without it. It seems it's the combination of the organization plugin and the stripe plugin, because removing the organization plugin also removes the warning. If anyone has gotten around this issue, I'd love some pointers in the right direction 🙏
Jonathan Powers
Jonathan Powers3mo ago
Just here to add a +1 to this. The organization plugin is the culprit, removing other plugins and leaving it in place still displays the error. I assume this is because the organization plugin declaration is about 6500 lines of types. Disabling declaration did not help, however that wouldn't be an option for us anyway, as we are also using a monorepo with project references. The ReturnType<typeof betterAuth> workaround broke a bunch of other types that were based on config, so I wound up with this hack:
const options = {
// ... my options
}} as const satisfies BetterAuthOptions & {
organization: Record<string, unknown>
member: Record<string, unknown>
invitation: Record<string, unknown>
}

export const auth: ReturnType<
typeof betterAuth<
typeof options & {
plugins: BetterAuthPlugin[]
}
>
> = betterAuth(options)
const options = {
// ... my options
}} as const satisfies BetterAuthOptions & {
organization: Record<string, unknown>
member: Record<string, unknown>
invitation: Record<string, unknown>
}

export const auth: ReturnType<
typeof betterAuth<
typeof options & {
plugins: BetterAuthPlugin[]
}
>
> = betterAuth(options)
This is obviously fragile, but is working for right now. On version 1.2.12
Ping
Ping3mo ago
I think this is mostly a Better-Auth issue, not sure if there are any work-arounds, or not to my knowledge at least. I'm certain we will fix this in the future, most likely in a major release (since it's possible we will have breaking changes - not 100% on that though) so it might be some time away unfortunately.
Arthur
Arthur2mo ago
+1
braifz
braifz2mo ago
+1 I'm getting the same error when trying to add the organization plugin.
Azzani
Azzani3w ago
Same error, every time i try workaround, i get my self in another issue.

Did you find this page helpful?