Jieunny์ ๋ธ๋ก๊ทธ
[TS] Exercises 4 ๋ณธ๋ฌธ
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);
๐ก ํ์
ํจ์์ ๋ฆฌํด ํ์
์ ์ง์ ํด์ฃผ์ด์ ์ค๋ฅ๋ฅผ ํด๊ฒฐ ํ ์ ์๋ค.
์ฌ๊ธฐ์ ์ฌ์ฉํ ๋ฐฉ๋ฒ์ 'ํ์
๊ฐ๋' ๋ผ๊ณ ํ๋ค.
โ๏ธ 'ํ์ ๊ฐ๋' ๋ ๋ฌด์์ผ๊น?
- ๋ฐ์ดํฐ์ ํ์
์ ์ ์ ์๊ฑฐ๋, ๊ฐ๋ฅํ ํ์
์ด ์ฌ๋ฌ๊ฐ ์ผ ๊ฒฝ์ฐ, ์กฐ๊ฑด๋ฌธ์ ํตํด ํ์
์ ๋ฒ์๋ฅผ ์ขํ์ฃผ๋ ๊ฒ
- ํ์
์ ๋ฐ๋ผ ๋์ํ ์ ์์ด ์๋ฌ๋ฅผ ์ค์ผ ์ ์๋ค.
'Study > TypeScript' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TS] ๋ฆฌ์กํธ + ํ์ ์คํฌ๋ฆฝํธ + Redux (0) | 2023.09.14 |
---|---|
[TS] Exercises5 (0) | 2023.07.20 |
[TS] Exercises 1~3 (0) | 2023.07.18 |
[TS] ํจ์ ์กฐํฉ์ ์๋ฆฌ์ ์์ฉ (0) | 2023.02.09 |
[TS] Promise์ async/await ๊ตฌ๋ฌธ (0) | 2023.02.06 |