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