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,
Key extends string,
Format extends "output" | "input" = "output",
> = Options["plugins"] extends Array<infer T>
? T extends {
schema: {
[key in Key]: {
fields: infer Field;
> = Options["plugins"] extends []
? {}
: Options["plugins"] extends Array<infer T>
? T extends {
schema: {
[key in Key]: {
fields: infer Field;
};
};
};
}
? Format extends "output"
? InferFieldsOutput<Field>
: InferFieldsInput<Field>
: {}
: {};
}
? Format extends "output"
? InferFieldsOutput<Field>
: InferFieldsInput<Field>
: {}
: {};
export type InferFieldsFromOptions<
Options extends BetterAuthOptions,

View File

@@ -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",

View File

@@ -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",

View File

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

View File

@@ -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>();
});
});