-
Notifications
You must be signed in to change notification settings - Fork 0
π [3μ₯] μΈλ±μ€λ μμΈμ€ νμ μμ μ€λ₯ μ 보~!
const metaData: MetadataProps = {
author: "μ°μ°",
createdAt: 2024-06-06,
chapter: 3,
description: "μΈλ±μ€λ μμΈμ€ νμ
μμ μ€λ₯ μ 보~!"
}
const { chapter, description } = metaData;
3.2.4 μΈλ±μ€νΈ μμΈμ€ νμ (p.101) μμ μ μ€λ₯κ° μλ κ² κ°μ μ 보ν©λλ€~! κ·Έλ°λ° μ λνΌμ μ΄λΌ μ κ° νλ¦° λΆλΆμ΄ μμ μλ μμ΅λλ€,,γ γ
μλλ μ± μ μμ μΈλ° IDEμ κ·Έλλ‘ μμ±νλ©΄ μλ¬κ° μ¬λΏ λ°μν©λλ€.
const PromotionList = [
{ type: 'product', name: 'chicken' },
{ type: 'product', name: 'pizza' },
{ type: 'card', name: 'cheer-up' },
]
// 1) 'T' only refers to a type, but is being used as a value here.
type ElementOf<T> = typeof T[number]
// 2) 'PromotionList' refers to a value, but is being used as a type here. Did you mean 'typeof PromotionList'?
type PromotionItemType = ElementOf<PromotionList>
μ°μ 2λ²μ μλ¬ λ©μμ§ κ·Έλλ‘ νμ
μ΄ λ€μ΄κ° μ리μ κ°μ λ£μ΄μ λ°μνλ μλ¬λ‘ typeof PromotionList
λ‘ κ³ μΉλ©΄ ν΄κ²°λ©λλ€.
1λ²μ μλ¬ λ©μμ§μ λ°λ₯΄λ©΄ 2λ²κ³Ό λ°λ μν©μΌλ‘ κ°μ΄ λ€μ΄κ° μ리μ νμ
μ λ£μ΄μ λ°μνλ μλ¬μ
λλ€. typeof + κ° => νμ
μ΄ λλ κ±΄λ° κ° μ리μ μ λ€λ¦ νμ
Tλ₯Ό λ£μ΄μ μλ¬κ° λ°μνκ³ μμ΅λλ€. Tκ° νμ
μΈλ° κ΅³μ΄ μμ typeof
λ₯Ό λΆμΌ νμκ° μμ΅λλ€.
const PromotionList = [
{ type: 'product', name: 'chicken' },
{ type: 'product', name: 'pizza' },
{ type: 'card', name: 'cheer-up' },
]
// 3) Type 'number' cannot be used to index type 'T'.
type ElementOf<T> = T[number]
type PromotionItemType = ElementOf<typeof PromotionList>
νμ§λ§ μ¬μ ν 3λ² μλ¬κ° λ°μν©λλ€. μ΄λ Tμ ννλ₯Ό λͺ¨λ₯΄λλ° numberλ‘ μΈλ±μ€λ μμΈμ€λ₯Ό μλνκΈ° λλ¬Έμ λλ€. λ°λΌμ Tμ λ°°μ΄ ννλ§ μ€λλ‘ μ νν΄μΌ ν©λλ€.
const PromotionList = [
{ type: 'product', name: 'chicken' },
{ type: 'product', name: 'pizza' },
{ type: 'card', name: 'cheer-up' },
]
type ElementOf<T extends any[]> = T[number]
type PromotionItemType = ElementOf<typeof PromotionList>
μ΄μ (any μ¬μ©μ μ μΈνλ©΄) μλ¬λ ν΄κ²°λμ΅λλ€~!!
λ³κ°λ‘ ElementOf μν μ νλ μ νΈλ¦¬ν° νμ μ 5μ₯μ inferλ₯Ό μ¬μ©ν΄λ λ§λ€ μ μμ΅λλ€. μ΄λ¬λ©΄ anyλ₯Ό μ μΈ μ μκ² λ€μ!
type ElementOf<T> = T extends (infer E)[] ? E : never
β¨π λ¬Έμ λͺ¨μ πβ¨
π [2μ₯] enumμ μ¬μ©ν΄μΌ ν κΉ?
π [2μ₯] νμ
μ§ν©μ κ°λ
κ³Ό νμ
νΈνμ±
π [2μ₯] 맡λ νμ
μ΄ λκΉ?
π [2μ₯] Typeκ³Ό Interfaceλ κ°κ° μΈμ μ¬μ©νλ κ±ΈκΉ? μ λν μ견
π [3μ₯] μΈλ±μ€λ μμΈμ€ νμ
μμ μ€λ₯ μ 보~!
π [4μ₯] νμ
μ’νκΈ°μ λ°©μ(νμ
κ°λvs νμ
λ¨μΈ)