Skip to content

Instantly share code, notes, and snippets.

@BetterProgramming
Created November 8, 2022 02:05
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/7c9a103bdaae19b23d70a9399ccfb43f to your computer and use it in GitHub Desktop.
Save BetterProgramming/7c9a103bdaae19b23d70a9399ccfb43f to your computer and use it in GitHub Desktop.
type Person = {
birthday: Date;
};
type Car = {
yearOfConstruction: Date;
};
function calcAge(
input: Date | null | undefined | string | Person | Car
): number {
if (!input) {
return 0;
} else if (typeof input === "string") {
return diffInYears(parse(input));
} else if (input instanceof Date) {
return diffInYears(input);
} else if (input.type === "Car") {
return diffInYears(input.yearOfConstruction);
} else {
return diffInYears(input.birthday);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment