Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created November 8, 2022 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BetterProgramming/5c06278caecf7b0fb7e0174519e5c1fc to your computer and use it in GitHub Desktop.
Save BetterProgramming/5c06278caecf7b0fb7e0174519e5c1fc to your computer and use it in GitHub Desktop.
function assertNonNullable<T>(value: T): asserts value is NonNullable<T> {
if (value === undefined || value === null) {
throw new Error("undefined or null are not allowed");
}
}
function calcAge(
input: Date | null | undefined | string | Person | PersonJson
): number {
assertNonNullable(input);
if (typeof input === "string") {
return diffInYears(parse(input));
} else if (input instanceof Date) {
return diffInYears(input);
} else if (isPerson(input)) {
return diffInYears(input.birthday);
} else {
return diffInYears(parse(input.birthday));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment