Empty plugin array throws type inference into a never state

I setup a project recently and noticed I couldn't get inference on my Session types. After some time i figured out that having the plugin array empty on Better-auth's instance setup was the culprit. here's a minimal repro that should yield a type error from the LSP perceptive.
import { betterAuth } from "./packages/better-auth/src/auth";
import { expectTypeOf, describe, it } from "vitest";

// Instance with empty plugins array
const authWithEmptyPlugins = betterAuth({
plugins: [],
secret: "test-secret",
emailAndPassword: {
enabled: true,
},
});

// No plugins property
const authWithoutPlugins = betterAuth({
secret: "test-secret",
emailAndPassword: {
enabled: true,
},
});

type SessionWithEmptyPlugins = typeof authWithEmptyPlugins.$Infer.Session;
type SessionWithoutPlugins = typeof authWithoutPlugins.$Infer.Session;

describe("Empty Plugins Type Inference", () => {
it("should have equal types for empty plugins and no plugins (server)", () => {
// Type error here
expectTypeOf<SessionWithEmptyPlugins>().toEqualTypeOf<SessionWithoutPlugins>();
});
});
import { betterAuth } from "./packages/better-auth/src/auth";
import { expectTypeOf, describe, it } from "vitest";

// Instance with empty plugins array
const authWithEmptyPlugins = betterAuth({
plugins: [],
secret: "test-secret",
emailAndPassword: {
enabled: true,
},
});

// No plugins property
const authWithoutPlugins = betterAuth({
secret: "test-secret",
emailAndPassword: {
enabled: true,
},
});

type SessionWithEmptyPlugins = typeof authWithEmptyPlugins.$Infer.Session;
type SessionWithoutPlugins = typeof authWithoutPlugins.$Infer.Session;

describe("Empty Plugins Type Inference", () => {
it("should have equal types for empty plugins and no plugins (server)", () => {
// Type error here
expectTypeOf<SessionWithEmptyPlugins>().toEqualTypeOf<SessionWithoutPlugins>();
});
});
No description
No description
2 Replies
frectonz
frectonz3w ago
I have opened a PR for this, let me know what you think. https://github.com/better-auth/better-auth/pull/4612
GitHub
fix: infer types for empty list plugins correctly by frectonz · Pu...
Make sure the following infer the same types. const auth = betterAuth({ plugins: [] }); and const auth = betterAuth({ }); Summary by cubic Ensure betterAuth({ plugins: [] }) infers the same t...
TheGB0077
TheGB0077OP3w ago
Seems like the issue was fixed. Cheers bud!

Did you find this page helpful?