Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Better Auth · Type Alias

InferSession

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.

How it works

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.

Syntax

type Session = typeof auth.$Infer.Session

Returns

An inferred session type containing session and user properties, with applicable configured and plugin-provided fields.

Key points

  • The alias can be derived from either the client SDK or the server auth object through $Infer.
  • Plugin extensions participate in the inferred session type, allowing consumers to retain plugin-specific typing.
  • Plugin-supplied auxiliary types may also flow through the same inference mechanism.
  • Server code can derive the session type directly from the configured Better Auth server instance.

Common mistakes

  • Treat the inferred type as a wrapper containing both the session data and user data, rather than only the database session record.
  • Additional configured fields are available in the inferred type, so avoid duplicating or manually narrowing them unless your client and server configurations differ.

Example: Infer a server session type

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.

When to use it

Type server session data

A server endpoint needs a reusable, type-safe annotation for the current authenticated session.

Type client session data

A client component consumes session data while preserving fields added by Better Auth configuration.

Propagate plugin fields

A plugin extends the user or session model and downstream code must see those additions.

Interview questions

QWhat does InferSession describe?

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.

QWhy prefer inference over a manually defined session interface?

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.

QHow can the type be inferred on client and server?

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.

  • Session
  • User
  • $Infer
  • inferAdditionalFields
  • customSession
  • getSession
  • useSession

Source: TypeScript. Checked 2026-10-15.