Good Code
The good version exposes only the fields the list API needs while keeping the response type tied to the domain model.
Lesson 09
Derive focused request and view types from domain types without exposing everything.
type PullRequest = {
id: string;
title: string;
authorId: string;
mergedAt: string | null;
internalScore: number;
};
type PullRequestListItem = Pick<PullRequest, "id" | "title" | "authorId">;
function serializePullRequest(item: PullRequest): PullRequestListItem {
// Pick exposes only the fields this boundary promises.
return {
id: item.id,
title: item.title,
authorId: item.authorId,
};
}type PullRequest = {
id: string;
title: string;
authorId: string;
mergedAt: string | null;
internalScore: number;
};
function serializePullRequest(item: PullRequest): PullRequest {
// Returning the domain object leaks internal fields.
return item;
}The good version exposes only the fields the list API needs while keeping the response type tied to the domain model.
The bad version returns the full domain object, including internal fields that a boundary should not promise to external callers.