Back to all reviewers

Leverage TypeScript null safety

browserbase/stagehand
Based on 5 comments
TypeScript

Use TypeScript's type system proactively to prevent null and undefined issues at compile time rather than handling them reactively at runtime. This includes using exhaustive pattern matching, explicit type annotations, and avoiding type casting that can hide null safety problems.

Null Handling TypeScript

Reviewer Prompt

Use TypeScript’s type system proactively to prevent null and undefined issues at compile time rather than handling them reactively at runtime. This includes using exhaustive pattern matching, explicit type annotations, and avoiding type casting that can hide null safety problems.

Key practices:

  • Use switch statements with exhaustive matching to ensure all cases are handled: default: { throw exhaustiveMatchingGuard(action); }
  • Provide explicit type annotations for complex types instead of relying on type casting: const result: AnthropicTransformedResponse instead of return result as T
  • Handle null values explicitly with proper fallbacks: const xpaths = elementId === null ? ["//body"] : selectorMap[elementId] ?? [];
  • Use proper array typing to prevent undefined access: Array<{ input: EvalInput; output?: boolean }> instead of generic arrays

Example of good null safety:

// Instead of type casting that hides null issues
return cachedResponse as T;

// Use explicit typing
const cachedResponse: AnthropicTransformedResponse | undefined = this.cache.get(...);
if (cachedResponse) {
  return cachedResponse;
}

This approach catches potential null/undefined issues during development rather than discovering them at runtime.

5
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions