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

[TS] Exercises5 ๋ณธ๋ฌธ

Study/TypeScript

[TS] Exercises5

Jieunny 2023. 7. 20. 17:39

exercises5

๐Ÿ“Œ Type ๊ตฌ์กฐ๋ฅผ ๋ณต์ œํ•˜์ง€ ์•Š๊ณ  filterUsers ํ•จ์ˆ˜ ์ •์˜๋ฅผ ์ˆ˜์ •ํ•˜์—ฌ ์ž…๋ ฅ์— ๋”ฐ๋ผ ํ•„์š”ํ•œ ์ „์ฒด User ์ •๋ณด๊ฐ€ ์•„๋‹Œ ํ•„์š”ํ•œ criteria๋งŒ ์ „๋‹ฌํ•ด๋ผ.

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'
    },
    {
        type: 'user',
        name: 'Wilson',
        age: 23,
        occupation: 'Ball'
    },
    {
        type: 'admin',
        name: 'Agent Smith',
        age: 23,
        role: 'Administrator'
    }
];

export const isAdmin = (person: Person): person is Admin => person.type === 'admin';
export const isUser = (person: Person): person is User => person.type === 'user';

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

export function filterUsers(persons: Person[], criteria: Partial<User>): User[] {
    return persons.filter(isUser).filter((user) => {
        const criteriaKeys = Object.keys(criteria) as (keyof User)[];
        return criteriaKeys.every((fieldName) => {
            return user[fieldName] === criteria[fieldName];
        });
    });
}

console.log('Users of age 23:');

filterUsers(
    persons,
    {
        age: 23
    }
).forEach(logPerson);

๐Ÿ’ก User์˜ ์ „์ฒด ์†์„ฑ์ด ์•„๋‹Œ ํ•„์š”ํ•œ ์†์„ฑ๋งŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ฒŒ ํƒ€์ž…์„ ์ง€์ •ํ•ด์ค€๋‹ค.

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์˜ ์œ ํ‹ธ๋ฆฌํ‹ฐ ํƒ€์ž…์˜ Partial์„ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค.

 

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

- ํƒ€์ž… ๋ณ€ํ™˜์„ ๋” ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

- Partial<Type>์€ Type์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์„ ํƒ์ ์œผ๋กœ ๋งŒ๋“œ๋Š” ํƒ€์ž…์„ ์ƒ์„ฑํ•œ๋‹ค.

    - ์ฃผ์–ด์ง„ ํƒ€์ž…์˜ ๋ชจ๋“  ํ•˜์œ„ ํƒ€์ž… ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋‚ด๋Š” ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    - Type์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๊ฑฐ๋‚˜, ์•„๋ฌด ํ”„๋กœํผํ‹ฐ๋„ ๊ฐ–์ง€ ์•Š์•„๋„ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š์ง€๋งŒ, Type์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„๋‹Œ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๋ฉด ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.

- Required<Type>, Readonly<Type>, Record<Keys,Type> ๋“ฑ์ด ์žˆ๋‹ค.

 

'Study > TypeScript' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[TS] TodoList CRUD  (0) 2023.10.17
[TS] ๋ฆฌ์•กํŠธ + ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ + Redux  (0) 2023.09.14
[TS] Exercises 4  (0) 2023.07.19
[TS] Exercises 1~3  (0) 2023.07.18
[TS] ํ•จ์ˆ˜ ์กฐํ•ฉ์˜ ์›๋ฆฌ์™€ ์‘์šฉ  (0) 2023.02.09