/*
[ λ¬Έμ μκ° ]
λ€μ μꡬμ¬νμ λ§μ‘±νλ ꡬν μκ·Έλμ³λ₯Ό μμ±νμΈμ(맀κ°λ³μ νμ
λ§ μ μνλ©΄ λ©λλ€)
- μ΄λ―Έ μμ±λμ΄ μλ 3κ°μ μ€λ²λ‘λ μκ·Έλμ³λ₯Ό λͺ¨λ λ§μ‘±ν΄μΌ ν©λλ€.
- a, b, cμ κ°μ λͺ¨λ λν κ°μ λ°νν©λλ€.
- λ§μ½ 맀κ°λ³μμ κ°μ΄ undefinedμΌ κ²½μ° λͺ¨λ 0μΌλ‘ μ·¨κΈν©λλ€.
- aκ° μμ κ²½μ° : 0
- bκ° μμ κ²½μ° : 0
- cκ° μμ κ²½μ° : 0
*/
function add(): number;
function add(a: number): number;
function add(a: number, b: number, c: number): number;
/* [Quiz] ꡬν μκ·Έλμ³λ₯Ό μμ±νμΈμ */
function add(a, b, c) {
return (a ?? 0) + (b ?? 0) + (c ?? 0);
}
function add(a?:number, b?:number, c?:number) {
return (a ?? 0) + (b ?? 0) + (c ?? 0);
}
/*
[ λ¬Έμ μκ° ]
λ€μ μꡬμ¬νμ λ§μ‘±νλλ‘ μ¬μ©μ μ μ νμ
κ°λλ₯Ό μμ±νμΈμ
- isGuest ν¨μκ° trueλ₯Ό λ°ννλ©΄ 맀κ°λ³μλ‘ μ λ¬λ userλ Guest νμ
μμ΄ λ³΄μ₯λ©λλ€.
- isMember ν¨μκ° trueλ₯Ό λ°ννλ©΄ 맀κ°λ³μλ‘ μ λ¬λ userλ Member νμ
μμ΄ λ³΄μ₯λ©λλ€.
*/
/* [Quiz] μ¬μ©μ μ μ νμ
κ°λλ₯Ό μμ±νμΈμ(νμ
μμ X) */
type Guest = {
visitCount: number;
};
type Member = {
id: string;
};
type User = Guest | Member;
function isGuest(user: User) {}
function isMember(user: User) {}
/* [Test] μ¬κΈ°λΆν°λ μ λ΅μ 체ν¬νκΈ° μν μ©λλ‘ μμ νμ§ μμ΅λλ€ */
function inviteUser(user: User) {
if (isGuest(user)) {
console.log(`${user.visitCount}λ²μ§Έ λ°©λ¬Έμ
λλ€`);
} else if (isMember(user)) {
console.log(`${user.id}λ μλ
νμΈμ!`);
}
}
function isGuest(user: User): user is Guest {
return (user as Guest).visitCount != undefined;
}
function isMember(user: User): user is Member {
return (user as Member).id != undefined;
}
/*
[ λ¬Έμ μκ° ]
λ€μ 쑰건μ λ§μ‘±νλ 3κ°μ§ μΈν°νμ΄μ€λ₯Ό μΆκ°λ‘ ꡬννμΈμ
- λͺ¨λ μΈν°νμ΄μ€λ Person μΈν°νμ΄μ€λ₯Ό νμ₯ν©λλ€(extends).
1. Student μΈν°νμ΄μ€λ grade νλ‘νΌν°λ₯Ό κ°μ΅λλ€.
- grade νλ‘νΌν°μλ "A", "B", "C"λ§ μ μ₯ν μ μμ΅λλ€.
2. Developer μΈν°νμ΄μ€λ skills νλ‘νΌν°λ₯Ό κ°μ΅λλ€.
- skills νλ‘νΌν°μλ λ¬Έμμ΄ λ°°μ΄μ μ μ₯ν μ μμ΅λλ€.
3. Boss μΈν°νμ΄μ€λ company νλ‘νΌν°λ₯Ό κ°μ΅λλ€.
- company νλ‘νΌν°μλ λ¬Έμμ΄μ μ μ₯ν μ μμ΅λλ€.
*/
/* [Quiz] μ¬μ©μ μ μ νμ
κ°λλ₯Ό μμ±νμΈμ(νμ
μμ X) */
interface Person {
name: string;
}
/* [Test] μ¬κΈ°λΆν°λ μ λ΅μ 체ν¬νκΈ° μν μ©λλ‘ μμ νμ§ μμ΅λλ€ */
import { Equal, Expect, NotAny } from "@type-challenges/utils";
const student: Student = {
name: "μ΄μ ν",
grade: "A",
};
const developer: Developer = {
name: "μ΄μ ν",
skills: ["React", "TypeScript"],
};
const boss: Boss = {
name: "μ΄μ ν",
company: "νμ
μ€νλμ€",
};
type TestCases = [
Expect<Equal<Student["grade"], "A" | "B" | "C">>,
Expect<Equal<Developer["skills"], string[]>>,
Expect<Equal<Boss["company"], string>>
];
interface Student extends Person{
grade: "A"|"B"|"C";
}
interface Developer extends Person{
skills: string[];
}
interface Boss extends Person{
company: string;
}