Created
December 6, 2023 06:19
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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