import React from 'react'; type Person = { readonly id: number, readonly name: string, readonly profession: Profession, readonly accomplishment: string, readonly imageId: string } type People = Person[] type Profession = | 'mathematician' | 'chemist' | 'physicist' | 'astrophysicist' type GroupedProfession = { [Key in U as `${Key}s`]: Person[]; } & { everyone: Person[]; } const people: People = [{ id: 0, name: 'Creola Katherine Johnson', profession: 'mathematician', accomplishment: 'spaceflight calculations', imageId: 'MK3eW3A', }, { id: 1, name: 'Mario José Molina-Pasquel Henríquez', profession: 'chemist', accomplishment: 'discovery of Arctic ozone hole', imageId: 'mynHUSa', }, { id: 2, name: 'Mohammad Abdus Salam', profession: 'physicist', accomplishment: 'electromagnetism theory', imageId: 'bE7W1ji', }, { id: 3, name: 'Percy Lavon Julian', profession: 'chemist', accomplishment: 'pioneering cortisone drugs, steroids and birth control pills', imageId: 'IOjWm71', }, { id: 4, name: 'Subrahmanyan Chandrasekhar', profession: 'astrophysicist', accomplishment: 'white dwarf star mass calculations', imageId: 'lrWQx8l', }]; export function getImageUrl(person: Person) { return ( 'https://i.imgur.com/' + person.imageId + 's.jpg' ); } type ListPeopleProps = { title: string; people: People; } const ListPeople: React.FC = ({title, people}) => { const listItems = people.map(person =>
  • {person.name}

    {person.name}: {' ' + person.profession + ' '} known for {person.accomplishment}

  • , ); return (

    {title}

      {listItems}
    ); }; type Entries = { [K in keyof T]: [K, T[K]] }[keyof T]; const firstPerson = people[0]; const res = Object.entries(firstPerson) as Entries[]; for (const [key, value] of res) { if (key === 'profession') { console.log(key, value); } } export default function List() { const groups = Object.groupBy, Person>(people, p => p.profession === 'chemist' ? 'chemists' : 'everyone', ); const chemists = groups.chemists ?? []; const everyone = groups.everyone ?? []; return ( ); }