typescript not recognizing role field in astro locals with better auth

added a role field via additionalFields in auth.ts. At runtime, isAuthed.user from auth.api.getSession correctly includes role, but TypeScript doesn’t recognize it on Astro.locals.user. My env.d.ts uses import("better-auth").User, which lacks custom fields. Tried inferAdditionalFields<typeof auth>() on the client, but it didn’t help Astro’s types. Any advice on aligning the types properly?
3 Replies
KiNFiSH
KiNFiSH2w ago
ig you can create a type declaration file that merges the Better Auth types with your custom fields:
env.d.ts
/// <reference types="astro/client" />
import type { User } from "better-auth";

declare namespace App {
interface Locals {
user: User & {
role: string;
};
}
}
env.d.ts
/// <reference types="astro/client" />
import type { User } from "better-auth";

declare namespace App {
interface Locals {
user: User & {
role: string;
};
}
}
have you used infer inferAdditionalFields either ?
bkyerv
bkyervOP2w ago
Yeah I tried inferadditional field as suggested in the docs regarding typescript - didnt work for me. Maybe it was me, I tend to make mistakes in the code. Yeah I did eventually add hard coded type in the .env.dts file but thought maybe there is a better way than just manually adding fields to suppress ts warnings

Did you find this page helpful?