Back to all reviewers

prefer type guards

twentyhq/twenty
Based on 5 comments
TypeScript

Use semantic type guards like `isDefined()` instead of basic type checks (`typeof`, simple null checks) or unsafe type assertions (`as!`, `!`). Type guards provide better readability, type safety, and null reference prevention.

Null Handling TypeScript

Reviewer Prompt

Use semantic type guards like isDefined() instead of basic type checks (typeof, simple null checks) or unsafe type assertions (as!, !). Type guards provide better readability, type safety, and null reference prevention.

Prefer this:

// Use semantic type guards
if (!isDefined(activeTabId)) return;
if (!isDefined(pageLayout)) {
  throw new PageLayoutException(/*...*/);
}

// Filter out null values
return objectNameSingulars
  .map(name => objectMetadataItems.find(item => item.nameSingular === name))
  .filter(isDefined);

Instead of:

// Avoid basic type checks and unsafe assertions
if (!activeTabId) return;  // unclear intent
if (typeof parsed === 'object') { /*...*/ }  // prefer specific type guards
return restoredPageLayout!;  // unsafe assertion
filteredFields[key] = { /*...*/ } as FieldOutputSchemaV2;  // forced typing

This approach eliminates null reference errors, makes code intent clearer, and leverages TypeScript’s type system for better compile-time safety.

5
Comments Analyzed
TypeScript
Primary Language
Null Handling
Category

Source Discussions