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.
Enter the URL of a public GitHub repository