Created
January 21, 2021 15:59
-
-
Save Chester97/9ef89b4f6f49552b90de77823d71987d 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
interface User { | |
name: string; | |
age: number; | |
occupation: string; | |
} | |
interface Admin { | |
name: string; | |
age: number; | |
role: string; | |
} | |
export type Person = User | Admin; | |
export const persons: Person[] = [ | |
{ | |
name: 'Max Mustermann', | |
age: 25, | |
occupation: 'Chimney sweep' | |
}, | |
{ | |
name: 'Jane Doe', | |
age: 32, | |
role: 'Administrator' | |
}, | |
{ | |
name: 'Kate Müller', | |
age: 23, | |
occupation: 'Astronaut' | |
}, | |
{ | |
name: 'Bruce Willis', | |
age: 64, | |
role: 'World saver' | |
} | |
]; | |
export function logPerson(person: Person) { | |
let additionalInformation: string; | |
if ('role' in person) { | |
additionalInformation = person.role; | |
} else { | |
additionalInformation = person.occupation; | |
} | |
console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment