Skip to content
Architecture & TypesChapter 16 / 57Intermediate6 min read

Types Beside Logic

A practical way to organize TypeScript types. Give each module a .types.ts file for its types, constants and messages, and keep the logic separate.

On this page

1. What is this practice?

The logic file only does work: functions, queries, calculations. It does not declare its own types, it imports them from posts.types.ts.

bash
src/lib/posts/posts.ts
src/lib/posts/posts.types.ts

This is "beside, not inside". Types live next to the logic (not in a separate global folder), but not inside the logic file.

2. Why does it matter?

A real problem: a client component only needed MAX_TITLE_LENGTH. The developer imported it from posts.ts. But posts.ts also imports the database client and secret keys, so the bundler started pulling all the server code into the browser bundle and the build broke. It is even worse if a module with secrets reaches the client by accident.

A .types.ts file has no runtime dependencies, so the server, the client, tests and scripts can all import it safely.

Types inside the logic fileTypes beside the logic
Getting one constant means importing the whole server moduleA light types file is imported, with no side effects
Scrolling a 300 line file to find a typeEach module's contract sits in one small file
The same error message string written in three placesOne message constant, used by the route and the test
A global types folder becomes a dumping groundEach type sits next to its module, the owner is clear
Delete a module and its old types stay behindDelete the folder and the types go with it

3. When to follow it

LevelHow importantWhy
Learning projectA good habit, but not required in a project with one or two filesLearn to write types first. Start splitting once a file passes 100 lines
Production codeA must for every src/lib module and every componentSafe sharing between client and server, and each module's contract at a glance
Senior levelEnforce the convention in lint and reviewWith two conventions in one folder, imports turn into guesswork

4. Bad vs good code

Bad: everything in one file

ts
import { db } from "@/lib/db";

export const MAX_TITLE_LENGTH = 120;

export type Post = {
  id: string;
  title: string;
  status: "draft" | "published";
};

export async function publish(id: string): Promise<Post> {
  const post = await db.post.findUnique({ where: { id } });

  if (!post) {
    throw new Error("Post not found");
  }

  return db.post.update({ where: { id }, data: { status: "published" } });
}

Good: types beside logic

ts
export const MAX_TITLE_LENGTH = 120;

export const POST_NOT_FOUND = "Post not found";

export type PostStatus = "draft" | "published";

export type Post = {
  id: string;
  title: string;
  status: PostStatus;
};

The file above is src/lib/posts/posts.types.ts. Now the logic file:

ts
import { db } from "@/lib/db";

import { POST_NOT_FOUND, type Post } from "./posts.types";

export async function publish(id: string): Promise<Post> {
  const post = await db.post.findUnique({ where: { id } });

  if (!post) {
    throw new Error(POST_NOT_FOUND);
  }

  return db.post.update({ where: { id }, data: { status: "published" } });
}

A client component can now take MAX_TITLE_LENGTH from @/lib/posts/posts.types without touching the database client.

What is the difference?

ThingBad codeGood code
Importing a constantPulls in the whole server moduleOnly the light types file
Error messageA literal string, repeated everywhereThe POST_NOT_FOUND constant, one source
Logic fileTypes and logic mixedLogic only
TestCopies the message stringImports the same constant

5. How to apply it

When creating a new module
Create a folder named after the module, like src/lib/posts
Put types, constants and messages in posts.types.ts
Put only logic in posts.ts, importing the types
If there are many pure helpers, add posts.library.ts
Clients and tests take constants only from the types file

The same pattern works for components: src/components/PostCard/PostCard.tsx and PostCard.types.ts holding the props type.

What goes in .types.ts: type, interface, const objects that act like enums, limits, status values, user-facing error messages. What does not go in: any function that does work, any import that has side effects.

6. Exceptions and common mistakes

Many teams write types at the top of the same file, and in small modules that works fine. Some teams keep one global types/ folder. The trade-off: same-file types are quick at first, but trouble starts when a client needs a constant from a server module. In a global folder, ownership gets lost. The strict rule in this course: every module has its own .types.ts, from the start.

Database row types are not written by hand. They come from a generated file and .types.ts derives from them.

Mistake: Creating one big types.ts at the root

Why? Every module's types pile up in one file, it grows from a hundred lines to a thousand and nobody owns it.

Better: Keep each module's own module.types.ts inside that module's folder.

Mistake: Putting logic or side effects in the types file

Why? Then the types file gets heavy too, and importing it on the client becomes risky again.

Better: Only types, constants and messages in the types file. Use a separate library file for pure helper functions.

Mistake: Writing the same type in two places

Why? A field changes in one place and stays old in the other, and the drift quietly turns into a bug.

Better: One type in one place. Everything else imports it or derives from it with Pick and Omit.

7. What to learn before and after

Suggested order
Strict Typing
Types Beside Logic
Project Structure
Shared API Contracts

Final quick revision

CheckYes / No
Each module has its own folderYes
module.types.ts beside the logicYes
type declarations in the logic fileNo
Error messages in constantsYes
Runtime side effects in the types fileNo
The client takes constants only from the types fileYes