Space Complexity
Space complexity is how an algorithm's extra memory grows with input size. TypeScript types do not occupy runtime memory. The collections you choose — and whether you copy them to satisfy a type — do.
Detailed Explanation
A type annotation is erased. User, Readonly<User>, User & { role: "admin" } are the same bytes as the object you created. There is no wrapper, no tag, no extra field for the brand (unless you added a real field). Space complexity is about the arrays, maps, copies, and closures the JavaScript allocates.
Where types still steer space:
Copying to satisfy mutability. A function that requires T[] cannot take readonly T[]. Callers write [...items] to get a mutable copy. That copy is O(n) space, and it exists because of the signature. If the function only reads, accept readonly T[] and the copy goes away.
Copying to satisfy width. structuredClone or { ...user } to turn a User into a PublicUser allocates. Omit in the type system does not. If you only need to hide a field from the type, you can pass the same object as Omit<User, "passwordHash"> if you trust the caller not to read the extra field at runtime. If you do not trust them, you must allocate a new object. The type Omit will not strip the property.
Indexes. Building a Map<string, User> from an array is O(n) extra space for O(1) lookup. The type of the function that returns the map documents that the caller now owns an index. A function that builds the map internally and drops it has a hidden allocation.
Tuples vs objects. A [string, number] and a { id: string; cents: number } are both one object (arrays are objects). The space difference is not why you pick one. Pick for API, not for bytes.
Type-level structures (huge unions, recursive conditional types) cost compiler memory. They can make tsc peak RAM grow. That is not your server's heap. Simplify the type if the compiler is the process that is swapping.
When you write a generic algorithm, decide whether it is in-place (T[] mutated, O(1) extra) or allocating (readonly T[] in, T[] out). Put that decision in the signature. Readonly on the input plus a new array return is the allocating form. T[] input with no return is the in-place form. TypeScript will not stop you from doing both at once; reviewers should.
For the usual definitions of extra space vs input space, see the JavaScript space-complexity page.
Code Examples
1. A readonly input avoids a defensive copy.
// 1. A readonly input avoids a defensive copy.
function sum(values: readonly number[]): number {
let total = 0;
for (const n of values) total += n;
return total;
}
const locked: readonly number[] = [1, 2, 3];
console.log(sum(locked)); // 6 — no copy2. Omit is a type; stripping is an allocation.
// 2. Omit is a type; stripping is an allocation.
type User = { id: string; email: string; passwordHash: string };
type PublicUser = Omit<User, "passwordHash">;
function toPublic(user: User): PublicUser {
return { id: user.id, email: user.email }; // new object, hash not copied
}3. An index is extra O(n) space, visible in the return type.
// 3. An index is extra O(n) space, visible in the return type.
function indexById<T extends { id: string }>(rows: readonly T[]): Map<string, T> {
const map = new Map<string, T>();
for (const row of rows) map.set(row.id, row);
return map;
}Common Gotchas
Satisfying `T[]` from a `readonly T[]` with a spread is a hidden `O(n)` copy
change the parameter instead of copying at every call site.
`as PublicUser` on a full `User` does not free the extra fields
the object still holds `passwordHash`. Allocate if you must hide it from runtime readers.
Brands, `readonly`, and aliases add no bytes
do not "optimize space" by removing types.
A recursive type that the compiler expands can exhaust `tsc` memory
that is a compile-time problem; flatten the type.