Type server session data
A server endpoint needs a reusable, type-safe annotation for the current authenticated session.
Better Auth · Type Alias
InferSession is Better Auth’s inferred session type, derived through $Infer from a client or server auth instance. It includes both the session and user portions and incorporates supported configured or plugin-provided fields.
InferSession is a type alias for the session shape inferred from Better Auth configuration. It reflects the returned session container, including both the session record and associated user, and can incorporate fields supplied by plugins or configured additional fields.
type Session = typeof auth.$Infer.SessionAn inferred session type containing session and user properties, with applicable configured and plugin-provided fields.
$Infer.import { betterAuth } from "better-auth";
import Database from "better-sqlite3";
export const auth = betterAuth({
database: new Database(":memory:")
});
type Session = typeof auth.$Infer.Session;
export function readSession(session: Session) {
return session.session.expiresAt;
}This derives a session type from the configured server auth instance, then uses its session portion with type safety.
A server endpoint needs a reusable, type-safe annotation for the current authenticated session.
A client component consumes session data while preserving fields added by Better Auth configuration.
A plugin extends the user or session model and downstream code must see those additions.
It represents the inferred Better Auth session response, whose shape contains both session and user. The type is produced from the auth configuration, so plugin-provided and configured fields can be reflected.
Follow-up: How would you derive it on the server? Use the auth instance's inference property, such as typeof auth.$Infer.Session, after creating the Better Auth instance.
The inferred type is based on the configured auth instance rather than a fixed hand-written interface. This lets Better Auth carry configuration and plugin type information into session consumers.
Follow-up: What changes when extra user or session fields are configured? Those fields are inferred and become available on both server and client sides.
On the client, create the Better Auth client and derive the type through its $Infer.Session property. On the server, use the corresponding $Infer.Session property on the auth instance.
Follow-up: What if client and server live in separate projects? The additional fields must be specified manually when creating the auth client.
SessionUser$InferinferAdditionalFieldscustomSessiongetSessionuseSession