๐Ÿ’™ Type Challenges/Typescript Exercises

[TS] TypeScript Exercises 2 ํ•ด์„ ๋ฐ ํ’€์ด

์„ ๋‹ฌ 2023. 7. 3. 15:04
๋ฐ˜์‘ํ˜•

Introduce

    All 2 users liked the idea of the community. We should go
    forward and introduce some order. We are in Germany after all.
    Let's add a couple of admins.

    Initially, we only had users in the in-memory database. After
    introducing Admins, we need to fix the types so that
    everything works well together.

 

๋‘๋ช…์˜ ์œ ์ € ์ „๋ถ€ ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ์•„์ด๋””์–ด๊ฐ€ ๋งˆ์Œ์— ๋“ค์—ˆ๊ณ , ์šฐ๋ฆฌ๋Š” ์ด์ œ ์†Œ๊ฐœ๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. admin ๋ช‡๋ช…์„ ์ถ”๊ฐ€ํ•ด๋ด…์‹œ๋‹ค.

์ฒ˜์Œ์— ์šฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—๋งŒ ์œ ์ €๋ฅผ ์ €์žฅํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ admins๋“ค์„ ์†Œ๊ฐœํ•œ ์ดํ›„ ์šฐ๋ฆฌ๋Š” ๋ชจ๋“ ๊ฒƒ์ด ์ž˜ ์ž‘๋™๋  ์ˆ˜ ์žˆ๋„๋ก ํƒ€์ž…๋“ค์„ ๊ณ ์ณ์•ผํ•ฉ๋‹ˆ๋‹ค

 

Exercise

    Type "Person" is missing, please define it and use
    it in persons array and logPerson function in order to fix
    all the TS errors.

 

๋ฐœ์ƒํ•˜๊ณ  ์žˆ๋Š” ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์—๋Ÿฌ๋“ค์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด

์‚ฌ๋ผ์ง„ Person ํƒ€์ž…์„ ์ •์˜ํ•ด์„œ  persons ๋ฐฐ์—ด๊ณผ logPerson ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•ด์ฃผ์„ธ์š”

 

Code

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[] /* <- 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(user: Person) {
    console.log(` - ${user.name}, ${user.age}`);
}

persons.forEach(logPerson);

 

https://typescript-exercises.github.io/#exercise=2&file=%2Findex.ts

 

TypeScript Exercises

A set of interactive TypeScript exercises

typescript-exercises.github.io

 

๋ฐ˜์‘ํ˜•