mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 12:27:43 +00:00
fix: should infer types correctly when empty list of plugins is provided (#4612)
This commit is contained in:
@@ -274,7 +274,9 @@ export type InferFieldsFromPlugins<
|
||||
Options extends BetterAuthOptions,
|
||||
Key extends string,
|
||||
Format extends "output" | "input" = "output",
|
||||
> = Options["plugins"] extends Array<infer T>
|
||||
> = Options["plugins"] extends []
|
||||
? {}
|
||||
: Options["plugins"] extends Array<infer T>
|
||||
? T extends {
|
||||
schema: {
|
||||
[key in Key]: {
|
||||
|
||||
@@ -93,7 +93,7 @@ export async function getTestInstanceMemory<
|
||||
...options?.advanced,
|
||||
},
|
||||
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 = {
|
||||
email: "test@test.com",
|
||||
|
||||
@@ -109,7 +109,7 @@ export async function getTestInstance<
|
||||
...options?.advanced,
|
||||
},
|
||||
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 = {
|
||||
email: "test@test.com",
|
||||
|
||||
@@ -291,7 +291,7 @@ export type BetterAuthOptions = {
|
||||
/**
|
||||
* List of Better Auth plugins
|
||||
*/
|
||||
plugins?: BetterAuthPlugin[];
|
||||
plugins?: [] | BetterAuthPlugin[];
|
||||
/**
|
||||
* User configuration
|
||||
*/
|
||||
|
||||
@@ -55,4 +55,26 @@ describe("general types", async (it) => {
|
||||
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>();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user