This TypeScript error (TS7056) occurs when the compiler tries to serialize a type that has grown too large, typically exceeding TypeScript's internal limits for type serialization. It commonly happens with complex type inference in large codebases using libraries like tRPC, Zod, or Prisma.
TypeScript has internal limits on how large a type can be when it needs to serialize it for error messages, IntelliSense, or declaration file generation. When you work with complex type systems—especially those that generate types dynamically like tRPC routers, Zod schemas, or Prisma models—the inferred types can become massive. TypeScript tries to serialize these types for display or export, but hits a size limit and throws this error.
If using tRPC, remove Prisma or other large type dependencies from the context object. Instead, import them directly where needed:
// Instead of this:
const createContext = ({ prisma }: { prisma: PrismaClient }) => ({
prisma,
user: null,
});
// Do this:
const createContext = () => ({
user: null,
});
// Then import Prisma directly in procedures
import { prisma } from '~/server/db';For Zod schemas, annotate them with explicit types instead of relying on z.infer:
// Instead of this:
const userSchema = z.object({
id: z.string(),
name: z.string(),
// ... many more fields
});
type User = z.infer<typeof userSchema>;
// Do this:
type User = {
id: string;
name: string;
// ... type definition
};
const userSchema: z.ZodType<User> = z.object({
id: z.string(),
name: z.string(),
// ...
});The satisfies operator helps TypeScript infer types while keeping them generic:
// Instead of letting TypeScript infer the entire type:
const router = {
user: userRouter,
post: postRouter,
// ... many routers
};
// Use satisfies to maintain generic types:
const router = {
user: userRouter,
post: postRouter,
// ...
} satisfies Record<string, AnyTRPCRouter>;Break down massive type definitions into smaller, reusable pieces:
// Instead of one giant union:
type AllEntities = EntityA | EntityB | EntityC | /* ... 50 more */;
// Split into logical groups:
type UserEntities = User | Admin | Guest;
type ContentEntities = Post | Comment | Page;
type SystemEntities = Log | Audit | Metric;
type AllEntities = UserEntities | ContentEntities | SystemEntities;For extremely large Zod schemas, generate TypeScript types at build time instead of relying on inference:
# Install zod-to-ts
npm install zod-to-ts
# Generate types in a build script
import { generateType } from 'zod-to-ts';
import { largeSchema } from './schema';
const { code } = generateType(largeSchema, 'LargeType');
// Write to a .d.ts fileExamine where type complexity is coming from:
1. Look for type duplication in your codebase
2. Consider if you need all the type precision you're asking for
3. Use type aliases instead of repeating complex inline types
4. Consider using interface for object types as they can be more efficiently cached
5. Use mapped types and conditional types judiciously
This error is fundamentally about TypeScript's implementation limits. The compiler needs to serialize types for various purposes: error messages, declaration file generation, and language server features. When types become too complex—often due to combinatorial explosion in generic type inference—they exceed practical limits. While there's no official "maximum type size" documented, it's generally around 100KB per type node. The best long-term solution is to simplify your type architecture: prefer composition over inheritance, use explicit type annotations for public APIs, and avoid "type-only" dependencies in hot paths.
Function expression requires a return type
Function expression requires a return type
Value of type 'string | undefined' is not iterable
How to fix "Value is not iterable" in TypeScript
Type 'undefined' is not assignable to type 'string'
How to fix "Type undefined is not assignable to type string" in TypeScript
Type narrowing from typeof check produces 'never'
How to fix "Type narrowing produces never" in TypeScript
Type parameter 'T' has conflicting constraints
How to fix "Type parameter has conflicting constraints" in TypeScript