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 > Typescript Exercises' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[TS] TypeScript Exercises 5 ํด์ ๋ฐ ํ์ด (0) | 2023.07.17 |
---|---|
[TS] TypeScript Exercises 5 ํด์ ๋ฐ ํ์ด (0) | 2023.07.14 |
[TS] TypeScript Exercises 4 ํด์ ๋ฐ ํ์ด (0) | 2023.07.05 |
[TS] TypeScript Exercises 3 ํด์ ๋ฐ ํ์ด (0) | 2023.07.04 |
[TS] TypeScript Exercises 1 ํด์ ๋ฐ ํ์ด (0) | 2023.07.03 |