Good Code
The good version types the input boundary and lets the local label and returned object infer from real values.
Lesson 01
Let TypeScript infer local values, but type the boundaries other code depends on.
type ReviewSummaryInput = {
title: string;
commentCount: number;
state: "open" | "closed";
};
export function createReviewSummary(input: ReviewSummaryInput) {
// Boundary type documents what callers must provide.
const label = input.title + " (" + input.commentCount + ")";
return {
label,
isClosed: input.state === "closed",
};
}export function createReviewSummary(input: any) {
// Local annotations do not protect an any input boundary.
const label: string = input.title + " (" + input.comments.length + ")";
const isClosed: boolean = input.closed;
return {
label,
isClosed,
};
}The good version types the input boundary and lets the local label and returned object infer from real values.
The bad version annotates local variables while leaving the public input as any, so reviewers still cannot trust the shape that matters most.