[ν•œμž…] πŸ“’Day8 Quiz

TKΒ·2023λ…„ 12μ›” 15일
0

[κ°•μ˜] ν•œμž… μ‹œλ¦¬μ¦ˆ

λͺ©λ‘ 보기
33/59

Quiz1. κ΅¬ν˜„ μ‹œκ·Έλ‹ˆμ³

/*
[ 문제 μ†Œκ°œ ]
λ‹€μŒ μš”κ΅¬μ‚¬ν•­μ„ λ§Œμ‘±ν•˜λŠ” κ΅¬ν˜„ μ‹œκ·Έλ‹ˆμ³λ₯Ό μ™„μ„±ν•˜μ„Έμš”(λ§€κ°œλ³€μˆ˜ νƒ€μž…λ§Œ μ •μ˜ν•˜λ©΄ λ©λ‹ˆλ‹€)
- 이미 μž‘μ„±λ˜μ–΄ μžˆλŠ” 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);
}

μ½”λ“œλ§ν¬


Quiz2. μ‚¬μš©μž μ •μ˜ νƒ€μž… κ°€λ“œ

/*
[ 문제 μ†Œκ°œ ]
λ‹€μŒ μš”κ΅¬μ‚¬ν•­μ„ λ§Œμ‘±ν•˜λ„λ‘ μ‚¬μš©μž μ •μ˜ νƒ€μž… κ°€λ“œλ₯Ό μ™„μ„±ν•˜μ„Έμš”
- 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;
}

μ½”λ“œλ§ν¬


Quiz3. μΆ”κ°€ μΈν„°νŽ˜μ΄μŠ€

/*
[ 문제 μ†Œκ°œ ]
λ‹€μŒ 쑰건을 λ§Œμ‘±ν•˜λŠ” 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;
}

μ½”λ“œλ§ν¬

profile
μ‰¬μš΄κ²Œ 쒋은 FE개발자😺

0개의 λŒ“κΈ€