2024-05-16.md
π‘DIL: μ΄νν°λΈ νμ μ€ν¬λ¦½νΈ
μ€ν°λ: μκ° CS, https://github.com/monthly-cs/2024-05-effective-typescript
μμ±μΌ: 2024-05-16
μμ±μ: dusunax
μμ΄ν 8: νμ 곡κ°κ³Ό κ° κ³΅κ°μ μ¬λ² ꡬλΆνκΈ° Know How to Tell Whether a Symbol Is in the Type Space or Value Space
- symbolμ νμ
곡κ°(type space)μ΄λ κ° κ³΅κ°(value space) μ€ ν κ³³μ μμΉ
- μ΄λ¦μ΄ κ°λλΌλ, μνλ 곡κ°μ λ°λΌ λ€λ₯Έ κ²μ λνλΌ μ μλ€
νμ 곡κ°κ³Ό κ° κ³΅κ°μ μ¬λ²
interface Cylinder {
radius: number;
height: number;
} // Cylinderλ νμ
. Type space
const Cylinder = (radius: number, height: number) => ({ radius, height }); // Cylinderλ ν¨μ. Value space
// μ€λ₯ μν©
function calculateVolume(shape: unknown) {
if (shape instanceof Cylinder) {
shape.radius;
// ~~~~~~ Property 'radius' does not exist on type '{}'
// λ°νμμ Cylinder μ¬λ³Όμ κ°μΌλ‘ μ°μ°νλ € νμΌλ―λ‘, κ° κ³΅κ°μ ν¨μ Cylinderλ₯Ό μ°Έμ‘°νλ€
}
}
- symbolμ΄ valueμΈμ§ typeμΈμ§λ λ¬Έλ§₯μ μ΄ν΄ μμλ΄μΌ νλ€
- typescript playgroundλ₯Ό νμ©νλ©΄μ μ»΄νμΌλ μλ°μ€ν¬λ¦½νΈ κ²°κ³Όλ¬Όμ ν¨κ» 보면μ type space & value space κ°λ
μ‘κΈ°
- type, interfaceλ type
- const, let, varλ value
- μλ°μ€ν¬λ¦½νΈ μ»΄νμΌ ν μ κ±°λλ©΄ type μ 보
- μλ°μ€ν¬λ¦½νΈ μ»΄νμΌ ν λ¨μΌλ©΄ value μ 보
- νμ μ μΈ λλ λ¨μΈλ¬Έ λ€μμ λμ€λ μ¬λ²μ type
- = λ€μμ λμ€λ μ¬λ²μ value
- classμ enumμ μν©μ λ°λΌ type λλ value
interface Person {
first: string;
last: string;
}
const jane: Person = { first: "Jane", last: "Jacobs" };
// ββββ βββββββββββββββββββββββββββββββββ Values
// ββββββ Type
function email(to: Person, subject: string, body: string): Response {
// βββββ ββ βββββββ ββββ Values
// ββββββ ββββββ ββββββ ββββββββ Types
// ...
}
class, enum
- ν΄λμ€κ° νμ μΌλ‘ μ°μΌ λλ: νν(μμ±κ³Ό λ©μλ)κ° μ¬μ©λ¨
- ν΄λμ€κ° κ°μΌλ‘ μ°μΌ λλ μμ±μκ° μ¬μ©λ¨
class Cylinder {
radius: number;
height: number;
constructor(radius: number, height: number) {
this.radius = radius;
this.height = height;
}
}
function calculateVolume(shape: unknown) {
if (shape instanceof Cylinder) {
// Cylinderλ₯Ό κ°μΌλ‘ μ°μ°
// μμ±μκ° μ¬μ©λ¨
shape;
// ^? (parameter) shape: Cylinder, νμ
μΌλ‘ μ°Έμ‘°
// νν(μμ±κ³Ό λ©μλ)κ° μ¬μ©λ¨
shape.radius;
// ^? (property) Cylinder.radius: number, νμ
μΌλ‘ μ°Έμ‘°
}
}
- typeofλ
κ°
μ μ½μ΄μ,νμ
μ λ°νν©λλ€- type space(νμ 곡κ°)μ typeofλ subsetμΌλ‘ μ¬μ©ν μ μκ³ , type ꡬ문μΌλ‘ μ΄λ¦μ λΆμ΄λ μ©λλ‘ μ¬μ©ν μ μλ€.
- value spaceμμ, typeofλ μλ°μ€ν¬λ¦½νΈ λ°νμμ typeofμ΄ λλ€.
- 6κ°μ λ°νμ νμ λ§ μ‘΄μ¬ν΄μλ€. (string, number, boolean, undefined, object, function)
interface Person {
first: string;
last: string;
}
const jane: Person = { first: "Jane", last: "Jacobs" };
const email = (to: Person, subject: string, body: string) => {};
type T1 = typeof jane;
// ^? type T1 = Person
type T2 = typeof email;
// ^? type T2 = (to: Person, subject: string, body: string) => Response
const v1 = typeof jane; // Value is "object"
const v2 = typeof email; // Value is "function"
console.log(v1, v2); // "object", "function"
ν΄λμ€μ νμ
class Cylinder {
radius: number;
height: number;
constructor(radius: number, height: number) {
this.radius = radius;
this.height = height;
}
}
const v = typeof Cylinder; // Value is "function"
// ν΄λμ€κ° μλ°μ€ν¬λ¦½νΈμμλ ν¨μλ‘ κ΅¬νλκΈ° λλ¬Έμ, vλ functionμ΄λ€.
type T = typeof Cylinder; // Type is typeof Cylinder
// Tμ Cylinderλ μΈμ€ν΄μ€μ νμ
μ΄ μλλΌ, μμ±μ ν¨μμ΄λ€.
// Tλ₯Ό value spaceμμλ νμΈν μ μμ. (νμ
μ΄κΈ° λλ¬Έ)
console.log(v); // "function"
- μμ μ μ½λ(κ·Έλ°λ°? νμ
λ§ μ μΈνλ©΄ λ°νμ μ€λ₯: fn is not defined)
- declare
- declareλ νμ μ€ν¬λ¦½νΈμμ λ³μ, ν¨μ, ν΄λμ€ λ±μ΄ μ΄λ―Έ λ€λ₯Έ κ³³μμ μ μλμ΄ μλ€κ³ μ μΈνλ ν€μλ
- μ»΄νμΌλ¬μκ² ν΄λΉ μλ³μμ λν μ 보λ₯Ό μ 곡νκ³ μ€μ ꡬνμ μ 곡νμ§ μλ κ²
- InstaceType μ λλ¦μ μ¬μ©ν΄ μμ±μ νμ κ³Ό μΈμ€ν΄μ€ νμ μ μ ν
- declare
declare let fnA: T; // νμ
μ΄ μμ±μ typeof Cylinderμμ νμΈ κ°λ₯(fn is not defined)
let fnB: T = Cylinder; // μ μΈκ³Ό ν¨κ» κ°μ ν λΉν΄μ£Όλ©΄ ν¨μλ₯Ό μ€νν μ μμ
type fnC = InstanceType<typeof Cylinder>; // νμ
μ΄ μμ±μ typeof Cylinderμμ νμΈ κ°λ₯(fn is not defined)
const c = new fnB(10, 5);
console.log(v, c);