worldint
mathengi
worldint
전체 방문자
오늘
어제
  • 분류 전체보기 (152)
    • infra, cloud (4)
      • aws (4)
    • TIL,WIL(일간,주간 회고) (57)
    • 컴퓨터 공학 (5)
      • 정보통신 (3)
      • 컴퓨터 구조 (2)
    • Math (1)
      • linear algebra (0)
      • 명제와 집합 (1)
    • Operating System (8)
      • Linux Ubuntu (1)
    • programming (66)
      • c , c++ (9)
      • c# (0)
      • java (2)
      • javascript (14)
      • Python (4)
      • github (1)
      • programing terms (12)
      • html, css (2)
      • docker (3)
      • algorithm_datastructure (5)
      • database (11)
      • flutter(dart) (2)
    • 항해99 부트캠프 (7)
      • 사전교육 (7)
    • 보안관련 (1)

블로그 메뉴

    공지사항

    인기 글

    태그

    • Javascript
    • AWS
    • 디비데드락
    • MONGOOSE
    • EC2
    • NoSQL
    • db데드락
    • ci/cd
    • ec2 #코드디플로이 #리눅스
    • NVM
    • CloudFront
    • flutter #provider #error
    • nodejs
    • Blue/Green
    • MongoDB
    • node
    • docker

    최근 댓글

    최근 글

    티스토리

    hELLO · Designed By 정상우.
    worldint

    mathengi

    programming/javascript

    [typescript] typeof, keyof

    2023. 5. 30. 10:59

    1.typeof

    객체를 객체형태의 타입으로 만들어 주는 키워드

    const obj = {
      red : "apple",
      blue : "sky",
      yellow : "flower",
    }

    이런 객체가 있을 때 

    obj에 typeof를 사용하면 아래처럼 타입에 할당이 가능하다

    type A = typeof obj
    type A = {
      red : "apple";
      blue : "sky";
      yellow : "flower";
    }

    2.keyof

    객체형태의 type의 key들 로 union type을 만들어줌

    type A = {
      red : "apple";
      blue : "sky";
      yellow : "flower";
    }
    
    type union = keyof A
    
    //type union = "red" | "blue" | "yellow"

    keyof를 enum에도 사용할 수 있는데

    enum obj {
      red = "apple",
      yellow = "banana",
      green = "cucumber",
    }

    그때는 keyof typeof A이런식으로 사용해야 한다

    type Key = keyof typeof obj;
    
    let ob3: Key = "red";

    객체에는 keyof를 쓸 수 없고

    enum에는 keyof를 쓸 수는 있는데 아래처럼 저런 이상한 union type이 만들어지게 된댜

    type Key = keyof obj; //이렇게 하면 아래의 코드에서 에러가 나온다
    
    let ob1:Key = "red"
    
    //Type '"red"' is not assignable to type 'number | unique symbol | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | ... 26 more ... | "sup"'.

     

    'programming > javascript' 카테고리의 다른 글

    [typescript] import,export 구문  (0) 2024.07.17
    [nextjs] 환경변수  (0) 2023.07.10
    [rxjs 용어 정리] hot, cold Observable  (0) 2023.05.25
    [rxjs] rxjs의 테스트 코드 작성(미완성)  (0) 2023.05.25
    [typescript] 클래스 선언 방법  (0) 2023.04.23
      'programming/javascript' 카테고리의 다른 글
      • [typescript] import,export 구문
      • [nextjs] 환경변수
      • [rxjs 용어 정리] hot, cold Observable
      • [rxjs] rxjs의 테스트 코드 작성(미완성)
      worldint
      worldint
      공부한 내용들, 트러블 슈팅 용 블로그

      티스토리툴바