λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°

#1 Language πŸ‘„/1-3 Typescript

[Typescript] κ³ κΈ‰ νƒ€μž…

νƒ€μž… κ°€λ“œ Narrowing
  • μš”μ•½
    • typeof λ³€μˆ˜
    • 속성λͺ… in 였브젝트 자료
    • μΈμŠ€ν„΄μŠ€ instanceof λΆ€λͺ¨
  • νƒ€μž…κ°€λ“œλŠ” νŠΉμ • μ†μ„±μ΄λ‚˜ λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•˜κΈ° 전에
  • 그것이 μ‘΄μž¬ν•˜λŠ”μ§€ ν™•μΈν•˜κ±°λ‚˜ νƒ€μž…μ„ μ‚¬μš©ν•˜κΈ° 전에 이 νƒ€μž…μœΌλ‘œ μ–΄λ–€ μž‘μ—…μ„
  • μˆ˜ν–‰ν•  수 μžˆλŠ”μ§€λ₯Ό ν™•μΈν•˜λŠ” κ°œλ…
  • λŸ°νƒ€μž„ μ‹œ νŠΉμ • νƒ€μž…μœΌλ‘œ μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° 전에 ν•΄λ‹Ή νƒ€μž…μ„ κ²€μ‚¬ν•˜λŠ” μ½”λ“œ νŒ¨ν„΄
  • μž‘μ—…μ„ μˆ˜ν–‰ν•˜κΈ° 전에 νƒ€μž…μ„ κ²€μ‚¬ν•˜μ—¬ λŸ°νƒ€μž„ 였λ₯˜λ₯Ό λ°©μ§€ν•  수 있음
  • 객체의 경우, instanceofλ‚˜ in을 μ‚¬μš©
  • λ‹€λ₯Έ νƒ€μž…λ“€μ˜ 경우 typeofλ₯Ό μ‚¬μš©
// typeof

type Combinable = string | number;

function add(a: Combinable, b: Combinable){
    if(typeof a === 'string' || typeof b === 'string'){
        return a.toString() + b.toString()
    }
    return a + b;
}
  • μœ λ‹ˆμ˜¨ νƒ€μž…μ΄ μ§€λ‹Œ μœ μ—°μ„±μ„ ν™œμš©ν•  수 있게 ν•΄μ€Œ
  • λŸ°νƒ€μž„ μ‹œ μ½”λ“œκ°€ μ •ν™•ν•˜κ²Œ μž‘λ™ν•˜κ²Œ ν•΄μ€Œ
// in
// 속성은 in을 μ΄μš©ν•΄μ„œ 확인

const e1: ElevatedEmployee = {
  name: "yumi",
  privileges: ["create-server"],
  startDate: new Date(),
};

function printEmployeeInformation(emp: UnknownEmployee) {
  console.log("Name: " + emp.name);
  if ("privileges" in emp) {
    console.log("Privilages: " + emp.privileges);
  }
  if ("startDate" in emp) {
    console.log("startDate: " + emp.startDate);
  }
}

printEmployeeInformation(e1);
// instanceof νƒ€μž… κ°€λ“œ

class Car {
    drive() {
        console.log('Driving...');
        
    }
}

class Truck {
    drive() {
        console.log('Driving a truck...');
    }

    loadCargo(amount: number){
        console.log('Loading cargo...' + amount);
        
    }
}

type Vechicle = Car | Truck

const v1 = new Car()
const v2 = new Truck()

function useVechicle(Vechicle: Vechicle) {
    Vechicle.drive()
    if(Vechicle instanceof Truck){
        Vechicle.loadCargo(1000)
    }
}

useVechicle(v1)
useVechicle(v2)
discriminated μœ λ‹ˆμ–Έ
  • νƒ€μž… κ°€λ“œλ₯Ό μ‰½κ²Œ κ΅¬ν˜„ν•  수 있게 ν•΄μ£ΌλŠ” μœ λ‹ˆμ–Έ νƒ€μž…μœΌλ‘œ μž‘μ—…μ„
  • μˆ˜ν–‰ν•  λ•Œ μ‚¬μš©ν•  수 μžˆλŠ” νŒ¨ν„΄
  • 객체 νƒ€μž…μœΌλ‘œ μž‘μ—…ν•  λ•Œλ„ μ‚¬μš© κ°€λŠ₯
  • κ²°λ‘ : 객체와 μœ λ‹ˆμ–Έ νƒ€μž…μ„ μ‚¬μš©ν•œ μž‘μ—… μ‹œ μ‚¬μš©ν•˜λŠ” νŒ¨ν„΄
interface Bird {
    type: 'bird'
    flyingSpeed: number
}

interface Horse {
    type: 'horse'
    runningSpeed: number
}

type Animal = Bird | Horse

function moveAnimal(animal: Animal) {
    let speed;
    switch (animal.type) {
        case 'bird':
            speed = animal.flyingSpeed
            break
            case 'horse':
                speed = animal.runningSpeed
    }
    console.log('Moving at speed: ' + speed);
    
}

moveAnimal({type: 'bird', flyingSpeed: 10})
  • μΈν„°νŽ˜μ΄μŠ€μ™€λ„ μž‘λ™ν•˜λŠ”λ°, 이 μΈν„°νŽ˜μ΄μŠ€λŠ” μΈν„°νŽ˜μ΄μŠ€λ₯Ό 기반으둜
  • κ΅¬μΆ•λœ λͺ¨λ“  객체가 이 νƒ€μž…μ„ 갖도둝 ν•˜κΈ° λ•Œλ¬Έ
  • μ£Όμ–΄μ§„ μ†μ„±μ˜ 쑴재 μ—¬λΆ€λ₯Ό ν™•μΈν•˜κ±°λ‚˜ instanceofλ₯Ό μ‚¬μš©ν•˜λŠ” 게 μ•„λ‹Œ
  • μ‹€μ œ μ‘΄μž¬ν•˜λŠ” 속성을 μ‚¬μš©ν•˜μ—¬ μ–΄λ–€ μœ ν˜•μ˜ 객체와 μž‘μ—…ν•˜κ³  μžˆλŠ”μ§€ 확인 κ°€λŠ₯
ν˜• λ³€ν™˜(typecasting)
  • ν˜• λ³€ν™˜μ€ νƒ€μž…μŠ€ν¬λ¦½νŠΈκ°€ 직접 κ°μ§€ν•˜μ§€ λͺ»ν•˜λŠ” νŠΉμ • νƒ€μž…μ˜ 값을 νƒ€μž…μŠ€ν¬λ¦½νŠΈμ— μ•Œλ €μ£ΌλŠ” μ—­ν• 
  • ν˜• λ³€ν™˜μ€ 두가지 ꡬ문으둜 κ΅¬ν˜„ν•  수 있음

1. λ³€ν™˜ν•˜κ³ μž ν•˜λŠ” μš”μ†Œ μ•žμ΄λ‚˜ νƒ€μž…μŠ€ν¬λ¦½νŠΈμ— νƒ€μž…μ„ μ•Œλ €μ£Όκ³ μž ν•˜λŠ” μœ„μΉ˜ μ•žμ— <HTMLInputElement> ν‚€μ›Œλ“œ μΆ”κ°€ν•˜λŠ” 방법

const userInputElement = <HTMLInputElement>document.getElementById('user-input')!

userInputElement.value = 'Hi there!'
  • 이 νƒ€μž…μ€ μ „μ—­μ μœΌλ‘œ μ‚¬μš© κ°€λŠ₯
  • tsconfig파일 내에 dom lib이 ν¬ν•¨λ˜μ–΄ 있기 λ•Œλ¬Έ

2. μ„ νƒν•œ λΆ€λΆ„ λ‹€μŒμ— as ν‚€μ›Œλ“œ μ‚¬μš©

const userInputElement = document.getElementById('user-input')! as HTMLInputElement

userInputElement.value = 'Hi there!'
  • λŠλ‚Œν‘œλ₯Ό μ‚¬μš©ν•˜μ—¬ λŠλ‚Œν‘œ μ•žμ˜ ν‘œν˜„μ‹μ„ null둜 λ°˜ν™˜ν•˜μ§€ μ•Šκ² λ‹€κ³  νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—κ²Œ μΈμ‹μ‹œν‚΄
  • 반면, 이것이 null을 λ°˜ν™˜ν•˜μ§€ μ•Šμ„ κ²ƒμ΄λΌλŠ” 확신이 λ“€μ§€ μ•ŠλŠ” 경우 if문을 μ‚¬μš©
// const userInputElement = <HTMLInputElement>document.getElementById('user-input')!
const userInputElement = document.getElementById('user-input');

if (userInputElement) {
    (userInputElement as HTMLInputElement).value = 'Hi there!'
}
인덱슀 속성
interface ErrorContainer{ // { email: 'Not a valid email', username: 'Must start with a character!}
    [prop: string]: string; // λͺ¨λ“  속성에 λ¬Έμžμ—΄ κ°’ νƒ€μž…μ΄ μžˆμ–΄μ•Ό ν•œλ‹€κ³  μ„€μ •
}

const errorBag: ErrorContainer = {
    // 1: 이라고 μž…λ ₯해도 μƒκ΄€μ—†μŒ. λͺ¨λ‘ λ¬Έμžμ—΄λ‘œ μΈμ‹ν•˜κΈ° λ•Œλ¬Έ
    email: 'Not a valid email!',
     username: 'Must start with a capital character!'
};
ν•¨μˆ˜ μ˜€λ²„λ‘œλ“œ
  • λ™μΌν•œ ν•¨μˆ˜μ— λŒ€ν•΄ μ—¬λŸ¬ ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜λ₯Ό μ •μ˜ν•  수 μžˆλŠ” κΈ°λŠ₯
  • κ°„λ‹¨νžˆ 말해, λ‹€μ–‘ν•œ λ§€κ°œλ³€μˆ˜λ₯Ό μ§€λ‹Œ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” μ—¬λŸ¬κ°€μ§€ 방법을 μ‚¬μš©ν•˜μ—¬
  • ν•¨μˆ˜ λ‚΄μ—μ„œ μž‘μ—…μ„ μˆ˜ν–‰ν•  수 있게 ν•΄μ€Œ
  • ν•¨μˆ˜ μ˜€λ²„λ‘œλ“œλŠ” μ£Όμš” ν•¨μˆ˜ λ°”λ‘œ μœ„μ— 같은 이름을 μž…λ ₯ν•˜λŠ” 것
  • 그리고 이 ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” 데 두 μΈμˆ˜κ°€ λͺ¨λ‘ μˆ«μžν˜•μ΄λ©΄ 이 ν•¨μˆ˜λŠ” μˆ«μžν˜•μ„ λ°˜ν™˜ν•œλ‹€κ³  νƒ€μž… μŠ€ν¬λ¦½νŠΈμ—κ²Œ μ•Œλ €μ€Œ
function add(a: number, b: number): number;
function add(a: string, b: string): string;
function add(a: string, b: number): string;
function add(a: number, b: string): string;

function add(a: Combinable, b: Combinable) {
  if (typeof a === "string" || typeof b === "string") {
    return a.toString() + b.toString();
  }
  return a + b;
}

const result = add("yuum", "schwarz");
result.split("");
  • νƒ€μž…μŠ€ν¬λ¦½νŠΈκ°€ 자체적으둜 λ°˜ν™˜ νƒ€μž…μ„ μ •ν™•νžˆ μΆ”λ‘ ν•˜μ§€ λͺ»ν•˜λŠ” κ²½μš°μ— μ‚¬μš©
optinal 체이닝
  • μ •μ˜λ˜μ–΄ μžˆλŠ”μ§€ μ—¬λΆ€κ°€ ν™•μ‹€μΉ˜ μ•Šμ€ μš”μ†Œ λ‹€μŒμ— λ¬ΌμŒν‘œλ₯Ό μΆ”κ°€
    • 이 κΈ°λŠ₯은 νƒ€μž…μŠ€ν¬λ¦½νŠΈ 3.7 μ΄μƒμ—μ„œλ§Œ 지원
  • 선택적 체이닝 μ—°μ‚°μžλŠ” 객체 λ°μ΄ν„°μ˜ μ€‘μ²©λœ 속성과 객체에 μ•ˆμ „ν•˜κ²Œ μ ‘κ·Όν•  수 있게 ν•΄μ€Œ
  • 사싀상 μ΄λŠ” 데이터에 μ ‘κ·Όν•˜κΈ° 전에 λ°μ΄ν„°μ˜ 쑴재 μ—¬λΆ€λ₯Ό ν™•μΈν•˜λŠ” if 문으둜 컴파일 됨
const fetchedUserData = {
  id: "u1",
  name: "yumi",
 // job: { title: "CEO", description: "My own company" },
};

console.log(fetchedUserData?.job?.title);
Null 병합
  • null 데이터 μ²˜λ¦¬μ— 도움을 쀌
  • μ–΄λ–€ λ°μ΄ν„°λ‚˜ μž…λ ₯값이 μžˆλŠ”λ° 그것이 null인지, undefined인지, μœ νš¨ν•œ 데이터인지 μ•Œ 수 μ—†λŠ” 경우 μ‚¬μš©
const userInput = " ";

const storedData = userInput ?? "DEFAULT";

console.log(storedData);
LIST