Skip to content

Instantly share code, notes, and snippets.

@manavm1990
Created December 6, 2023 06:19
Show Gist options
  • Save manavm1990/5e77fe036f55c51bb4c3666d9e74c4c0 to your computer and use it in GitHub Desktop.
Save manavm1990/5e77fe036f55c51bb4c3666d9e74c4c0 to your computer and use it in GitHub Desktop.
If 1️⃣ is using TS in a strict sense, many errors/warnings regarding 'possibly null,' etc. can be resolved by using this utility fxn.
export function isNotNullOrUndefinedOrEmpty<T>(
value: T | null | undefined,
): value is T {
switch (true) {
case value === null || value === undefined:
return false;
case typeof value === 'string':
return value.trim() !== '';
case Array.isArray(value):
return value.length > 0;
default:
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment