TypeScript

Lesson 01

Type inference and explicit boundaries

Let TypeScript infer local values, but type the boundaries other code depends on.

Good Code

review-summary.ts
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",
  };
}

Bad Code

review-summary.ts
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,
  };
}

Review Notes

What to review

Good Code

The good version types the input boundary and lets the local label and returned object infer from real values.

Bad Code

The bad version annotates local variables while leaving the public input as any, so reviewers still cannot trust the shape that matters most.

Takeaways

  • Annotate function inputs, return contracts, and exported APIs while letting local implementation details infer naturally.