• Tuple

    : 배열의 각 인덱스에 다른 타입을 지정하고 싶을 때

    const player: [string, number, boolean] = ['nico', 12, true];
    

    tuple + readonly

    const player: readonly [string, number, boolean] = ['nico', 12, true];
    
  • readonly

    : 값을 재할당하거나 변경할 수 없기 때문에 안전하게 값을 보관할 수 있다.

    (ex.1)

    type Player = {
      readonly name: String;
      age?: Number;
    };
    
    const playerMaker = (name: string): Player => ({ name });
    
    const nico = playerMaker('nico');
    nico.age = 12;
    
    nico.name = 'lll'; // errorMessage : 읽기 전용 속성이므로 'name'에 할당할 수 없습니다.
    

    (ex.2)

    Untitled

    Untitled

  • undnown

    let a: unknown;
    
    if (typeof a === 'number') {
      let b = a + 1;
    }
    
    if (typeof a === 'string') {
      let b = a.toUpperCase();
    }
    
    function hello() {
      console.log('x');
    }
    
  • never (feat. throw Error)

    : 함수가 절대 return 하지 않을 때 발생

    function hello(): never {
      throw new Error('xxx');
    }
    

    : 타입이 두 가지일 수 있을 때

    function hello(name: string | number) {
      if (typeof name === 'string') {
        name;
      } else if (typeof name === 'string') {
        name;
      } else {
        throw new Error('string or number로 입력해주세요');
      }
    }
    
    console.log(hello(true));