Recommended tsconfig
Better Auth requiresstrictNullChecks to be enabled for type inference to
work correctly. The easiest way to ensure this is to enable strict mode:
tsconfig.json
strict, enable strictNullChecks explicitly:
tsconfig.json
Inferring types with $Infer
Both the server instance and the client expose an $Infer namespace. Plugins
can extend base types like User and Session, and those extensions flow
through automatically.
Server-side
auth.ts
Client-side
auth-client.ts
Additional fields
You can add extra fields to theuser and session tables. All additional
fields are fully typed on both the server and client.
auth.ts
The input property
Inferring additional fields on the client
The client needs to know about server-side additional fields for its types to match. There are two approaches depending on your project structure.Same project (monorepo or single repo)
Use theinferAdditionalFields plugin and pass your server auth type:
auth-client.ts
Separate client and server projects
Specify the fields manually when creating the client:auth-client.ts
Plugin type inference
Plugins extendUser, Session, and other types transparently. Install a
plugin on the server and its types automatically propagate through $Infer.
auth.ts
$InferServerPlugin helper to mirror the server
plugin’s types:
client.ts
useSession type inference
The useSession hook (React, Vue, Svelte, Solid) returns the same inferred
Session type: