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

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

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

λͺ©λ‘ 보기
52/59
post-custom-banner

Quiz 1.

/*
[ 문제 μ†Œκ°œ ]
λ‹€μŒ μš”κ΅¬μ‚¬ν•­μ„ λ§Œμ‘±ν•˜λ„λ‘ getSellersFromProducts ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜μ™€ λ°˜ν™˜κ°’ νƒ€μž…μ„ μ •μ˜ν•˜μ„Έμš”
- getSellersFromProducts ν•¨μˆ˜λŠ” λ§€κ°œλ³€μˆ˜λ‘œ 받은 Product λ°°μ—΄λ‘œλΆ€ν„°, seller 객체만 μΆ”μΆœν•΄ μƒˆλ‘œμš΄ λ°°μ—΄λ‘œ λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜μž…λ‹ˆλ‹€.
- λ°˜ν™˜κ°’μ„ λͺ…μ‹œμ μœΌλ‘œ μ„€μ •ν•΄μ•Ό ν•©λ‹ˆλ‹€ (μΈλ±μŠ€λ“œ μ—‘μ„ΈμŠ€ νƒ€μž…)
*/

/* [Quiz] μ•„λž˜μ˜ μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ„Έμš” */

interface Product {
  id: number;
  name: string;
  price: number;
  seller: {
    id: number;
    name: string;
    company: string;
  };
}

function getSellersFromProducts(products: any): any {
  return products.map((product) => product.seller);
}

/* [Test] μ—¬κΈ°λΆ€ν„°λŠ” 정닡을 μ²΄ν¬ν•˜κΈ° μœ„ν•œ μš©λ„λ‘œ μˆ˜μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ */
import { Equal, Expect } from "@type-challenges/utils";

type TestCases = [
  Expect<Equal<Parameters<typeof getSellersFromProducts>[0], Product[]>>,
  Expect<Equal<ReturnType<typeof getSellersFromProducts>, Product["seller"][]>>
];

βœοΈλ‹΅μ•ˆ(ν‹€λ¦Ό)

function getSellersFromProducts(products: Product[]): Product["seller"] {
  return products.map((product) => product.seller);
}

μ •λ‹΅

function getSellersFromProducts(products: Product[]): Product["seller"][] {
  return products.map((product) => product.seller);
}

ν•΄μ„€

Quiz 2.


/*
[ 문제 μ†Œκ°œ ]
λ‹€μŒ 쑰건을 λ§Œμ‘±ν•˜λŠ” 3개의 νƒ€μž…μ„ μΆ”κ°€λ‘œ μ •μ˜ν•˜μ„Έμš”
- PartialProduct νƒ€μž…μ€ Product νƒ€μž…μ˜ λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό λ‹€ 선택적 ν”„λ‘œνΌν‹°λ‘œ λ°”κΎΌ νƒ€μž…μž…λ‹ˆλ‹€.
- ReadonlyProduct νƒ€μž…μ€ Product νƒ€μž…μ˜ λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό λ‹€ 읽기 μ „μš© ν”„λ‘œνΌν‹°λ‘œ λ°”κΎΌ νƒ€μž…μž…λ‹ˆλ‹€.
- ReadonlyPartialProduct νƒ€μž…μ€ Product νƒ€μž…μ˜ λͺ¨λ“  ν”„λ‘œνΌν‹°λ₯Ό λ‹€ 선택적, 읽기 μ „μš© ν”„λ‘œνΌν‹°λ‘œ λ°”κΎΌ νƒ€μž…μž…λ‹ˆλ‹€.
*/

/* [Quiz] μ•„λž˜μ˜ μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ„Έμš” */
interface Product {
  id: number;
  name: string;
  price: number;
  seller: {
    id: number;
    name: string;
    company: string;
  };
}

type PartialProduct = any;

type ReadonlyProduct = any;

type ReadonlyPartialProduct = any;

/* [Test] μ—¬κΈ°λΆ€ν„°λŠ” 정닡을 μ²΄ν¬ν•˜κΈ° μœ„ν•œ μš©λ„λ‘œ μˆ˜μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ */
import { Equal, Expect, NotEqual } from "@type-challenges/utils";

type TestCases = [
  Expect<Equal<PartialProduct, Partial<Product>>>,
  Expect<Equal<ReadonlyProduct, Readonly<Product>>>,
  Expect<Equal<ReadonlyPartialProduct, Readonly<Partial<Product>>>>
];

βœοΈλ‹΅μ•ˆ

type PartialProduct = {
  [key in keyof Product]?: Product[key];
};

type ReadonlyProduct = {
  readonly [key in keyof Product]: Product[key];
};

type ReadonlyPartialProduct = {
  readonly [key in keyof Product]?: Product[key];
};

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


Quiz 3.

/*
[ 문제 μ†Œκ°œ ]
λ‹€μŒ 쑰건을 λ§Œμ‘±ν•˜λŠ” Score νƒ€μž…μ„ κ΅¬ν˜„ν•˜μ„Έμš”
- Score νƒ€μž…μ€ 점수λ₯Ό λ‚˜νƒ€λ‚΄κΈ° μœ„ν•œ λ¬Έμžμ—΄ νƒ€μž…μœΌλ‘œ '${0λΆ€ν„° 10κΉŒμ§€μ˜ μ •μˆ˜}-${0λΆ€ν„° 10κΉŒμ§€μ˜ μ •μˆ˜}'ν˜•μ‹μ„ κ°–μŠ΅λ‹ˆλ‹€'
  - ex) "1-0", "3-2", "0-4" 이런 ν˜•νƒœμ˜ λ¬Έμžμ—΄ νƒ€μž…μž…λ‹ˆλ‹€.
  - ex) μ–΄λŠμͺ½μ˜ μ μˆ˜μ—λ„ 두 μžλ¦¬μˆ˜λŠ” 올 수 μ—†μŠ΅λ‹ˆλ‹€.
*/

/* [Quiz] μ•„λž˜μ˜ μ½”λ“œλ₯Ό μˆ˜μ •ν•˜μ„Έμš” */
type Score = any;

/* [Test] μ—¬κΈ°λΆ€ν„°λŠ” 정닡을 μ²΄ν¬ν•˜κΈ° μœ„ν•œ μš©λ„λ‘œ μˆ˜μ •ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ */
import { Equal, Expect, ExpectExtends, ExpectFalse } from "@type-challenges/utils";

const tc1 = "19-0";
const tc2 = "5-11";
const tc3 = "9-1";
const tc4 = "2-8";
const tc5 = "7-2";

type TestCases = [
  ExpectFalse<ExpectExtends<Score, typeof tc1>>,
  ExpectFalse<ExpectExtends<Score, typeof tc2>>,
  Expect<ExpectExtends<Score, typeof tc3>>,
  Expect<ExpectExtends<Score, typeof tc4>>,
  Expect<ExpectExtends<Score, typeof tc5>>
];

βœοΈλ‹΅μ•ˆ

type Score = `${Int}-${Int}`;

type Int = '1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'10';

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

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

0개의 λŒ“κΈ€