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
ig you can create a type declaration file that merges the Better Auth types with your custom fields:
have you used
infer inferAdditionalFields
either ?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
have you checked this - https://www.better-auth.com/docs/concepts/typescript#inferring-additional-fields-on-client
TypeScript | Better Auth
Better Auth TypeScript integration.