TypeScript

Lesson 06

Precise function types

Describe callbacks with the arguments and return value the caller actually supports.

Good Code

review-actions.ts
type ReviewAction = "approve" | "request-changes";
type ReviewHandler = (action: ReviewAction, comment: string) => void;

function submitReview(handleReview: ReviewHandler) {
  // The callback type limits actions and required arguments.
  handleReview("approve", "Ready to merge.");
}

Bad Code

review-actions.ts
function submitReview(handleReview: Function) {
  // Function accepts any call shape, so invalid calls compile.
  handleReview("approve");
  handleReview("delete-branch", 42, true);
}

Review Notes

What to review

Good Code

The good version documents exactly which actions and arguments the callback accepts.

Bad Code

The bad version accepts any callable value, so TypeScript cannot help reviewers spot missing arguments, unsupported actions, or unexpected return contracts.

Takeaways

  • Use precise function signatures to make invalid calls harder to express.