Jieunny์˜ ๋ธ”๋กœ๊ทธ

[TS] Exercises 4 ๋ณธ๋ฌธ

Study/TypeScript

[TS] Exercises 4

Jieunny 2023. 7. 19. 17:31

exercises4

๐Ÿ“Œ TypeScript๊ฐ€ ์ด ์ƒํ™ฉ์—์„œ type์„ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ˆ˜์ •์‚ฌํ•ญ์„ ์ ์šฉํ•ด๋ผ


interface User {
    type: 'user';
    name: string;
    age: number;
    occupation: string;
}

interface Admin {
    type: 'admin';
    name: string;
    age: number;
    role: string;
}

export type Person = User | Admin;

export const persons: Person[] = [
    { type: 'user', name: 'Max Mustermann', age: 25, occupation: 'Chimney sweep' },
    { type: 'admin', name: 'Jane Doe', age: 32, role: 'Administrator' },
    { type: 'user', name: 'Kate Mรผller', age: 23, occupation: 'Astronaut' },
    { type: 'admin', name: 'Bruce Willis', age: 64, role: 'World saver' }
];

export function isAdmin(person: Person) {
    return person.type === 'admin';
}

export function isUser(person: Person) {
    return person.type === 'user';
}

export function logPerson(person: Person) {
    let additionalInformation: string = '';
    if ("role" in person) {
        additionalInformation = person.role;
    }
    if ("occupation" in person) {
         additionalInformation = person.occupation;
     }
    console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`);
}

console.log('Admins:');
persons.filter(isAdmin).forEach(logPerson);

console.log();

console.log('Users:');
persons.filter(isUser).forEach(logPerson);

๐Ÿ’ก ๋ฆฌํ„ด๊ฐ’์— ํƒ€์ž…์ด ์ง€์ •๋˜์ง€ ์•Š์œผ๋ฉด isAdmin(person) ๊ฐ’์ด true๋ผ๋„ role์„ ํ• ๋‹นํ•  ์ˆ˜ ์—†๋‹ค.
'in' ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํƒ€์ž…์„ ์•Œ๋ ค์ฃผ์—ˆ๋‹ค.
 
์ด ๋ฐฉ๋ฒ•์œผ๋กœ pass๊ฐ€ ๋˜์„œ ๊ทธ๋Ÿผ 3๋ฒˆ ๋ฌธ์ œ๋ž‘ ๋‹ค๋ฅผ ๊ฒŒ ์—†์ง€์•Š๋‚˜? ์‹ถ์–ด์„œ ๊ตฌ๊ธ€๋ง ํ•ด ๋ณธ ๊ฒฐ๊ณผ 'is'๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ‘ธ๋Š” ๋ฌธ์ œ์˜€๋‹ค ใ…Ž.

interface User {
    type: 'user';
    name: string;
    age: number;
    occupation: string;
}

interface Admin {
    type: 'admin';
    name: string;
    age: number;
    role: string;
}

export type Person = User | Admin;

export const persons: Person[] = [
    { type: 'user', name: 'Max Mustermann', age: 25, occupation: 'Chimney sweep' },
    { type: 'admin', name: 'Jane Doe', age: 32, role: 'Administrator' },
    { type: 'user', name: 'Kate Mรผller', age: 23, occupation: 'Astronaut' },
    { type: 'admin', name: 'Bruce Willis', age: 64, role: 'World saver' }
];

export function isAdmin(person: Person): person is Admin {
    return person.type === 'admin';
}

export function isUser(person: Person): person is User {
    return person.type === 'user';
}

export function logPerson(person: Person) {
    let additionalInformation: string = '';
    if (isAdmin(person)) {
        additionalInformation = person.role;
    }
    if (isUser(person)) {
        additionalInformation = person.occupation;
    }
    console.log(` - ${person.name}, ${person.age}, ${additionalInformation}`);
}

console.log('Admins:');
persons.filter(isAdmin).forEach(logPerson);

console.log();

console.log('Users:');
persons.filter(isUser).forEach(logPerson);

๐Ÿ’ก ํƒ€์ž… ํ•จ์ˆ˜์˜ ๋ฆฌํ„ด ํƒ€์ž…์„ ์ง€์ •ํ•ด์ฃผ์–ด์„œ ์˜ค๋ฅ˜๋ฅผ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค.
์—ฌ๊ธฐ์„œ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์„ 'ํƒ€์ž… ๊ฐ€๋“œ' ๋ผ๊ณ  ํ•œ๋‹ค.
 

โ—๏ธ 'ํƒ€์ž… ๊ฐ€๋“œ' ๋ž€ ๋ฌด์—‡์ผ๊นŒ?

- ๋ฐ์ดํ„ฐ์˜ ํƒ€์ž…์„ ์•Œ ์ˆ˜ ์—†๊ฑฐ๋‚˜, ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์ด ์—ฌ๋Ÿฌ๊ฐœ ์ผ ๊ฒฝ์šฐ, ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ํƒ€์ž…์˜ ๋ฒ”์œ„๋ฅผ ์ขํ˜€์ฃผ๋Š” ๊ฒƒ
- ํƒ€์ž…์— ๋”ฐ๋ผ ๋Œ€์‘ํ•  ์ˆ˜ ์žˆ์–ด ์—๋Ÿฌ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.