fix: should infer types correctly when empty list of plugins is provided (#4612)

This commit is contained in:
Fraol Lemecha
2025-09-12 21:36:55 +03:00
committed by GitHub
parent ef4509af45
commit 08fa1f89c3
5 changed files with 39 additions and 15 deletions

View File

@@ -274,19 +274,21 @@ export type InferFieldsFromPlugins<
Options extends BetterAuthOptions, Options extends BetterAuthOptions,
Key extends string, Key extends string,
Format extends "output" | "input" = "output", Format extends "output" | "input" = "output",
> = Options["plugins"] extends Array<infer T> > = Options["plugins"] extends []
? T extends { ? {}
schema: { : Options["plugins"] extends Array<infer T>
[key in Key]: { ? T extends {
fields: infer Field; schema: {
[key in Key]: {
fields: infer Field;
};
}; };
}; }
} ? Format extends "output"
? Format extends "output" ? InferFieldsOutput<Field>
? InferFieldsOutput<Field> : InferFieldsInput<Field>
: InferFieldsInput<Field> : {}
: {} : {};
: {};
export type InferFieldsFromOptions< export type InferFieldsFromOptions<
Options extends BetterAuthOptions, Options extends BetterAuthOptions,

View File

@@ -93,7 +93,7 @@ export async function getTestInstanceMemory<
...options?.advanced, ...options?.advanced,
}, },
plugins: [bearer(), ...(options?.plugins || [])], plugins: [bearer(), ...(options?.plugins || [])],
} as O extends undefined ? typeof opts : O & typeof opts); } as unknown as O extends undefined ? typeof opts : O & typeof opts);
const testUser = { const testUser = {
email: "test@test.com", email: "test@test.com",

View File

@@ -109,7 +109,7 @@ export async function getTestInstance<
...options?.advanced, ...options?.advanced,
}, },
plugins: [bearer(), ...(options?.plugins || [])], plugins: [bearer(), ...(options?.plugins || [])],
} as O extends undefined ? typeof opts : O & typeof opts); } as unknown as O extends undefined ? typeof opts : O & typeof opts);
const testUser = { const testUser = {
email: "test@test.com", email: "test@test.com",

View File

@@ -291,7 +291,7 @@ export type BetterAuthOptions = {
/** /**
* List of Better Auth plugins * List of Better Auth plugins
*/ */
plugins?: BetterAuthPlugin[]; plugins?: [] | BetterAuthPlugin[];
/** /**
* User configuration * User configuration
*/ */

View File

@@ -55,4 +55,26 @@ describe("general types", async (it) => {
activeOrganizationId?: string | undefined | null; activeOrganizationId?: string | undefined | null;
}>(); }>();
}); });
it("should infer the same types for empty plugins and no plugins", async () => {
const { auth: authWithEmptyPlugins } = await getTestInstance({
plugins: [],
secret: "test-secret",
emailAndPassword: {
enabled: true,
},
});
const { auth: authWithoutPlugins } = await getTestInstance({
secret: "test-secret",
emailAndPassword: {
enabled: true,
},
});
type SessionWithEmptyPlugins = typeof authWithEmptyPlugins.$Infer;
type SessionWithoutPlugins = typeof authWithoutPlugins.$Infer;
expectTypeOf<SessionWithEmptyPlugins>().toEqualTypeOf<SessionWithoutPlugins>();
});
}); });