Jay's Cookbook
Menu
  • Tags
  • Categories
  • Projects
Computer Science
OS
Network
Data Structure
Algorithm
Language
Code Architecture
Python
Javascript
Typescript
Java
Backend
Backend Theory
TypeORM
Node.js
NestJS
FastAPI
Frontend
HTML/CSS
React
Next.js
Data Engineering
DE Theory
MySQL
MongoDB
Elastic
Redis
Kafka
Spark
Airflow
AI
Basic
Pytorch
NLP
Computer Vision
Data Analytics
Statistics
Pandas
Matplotlib
DevOps
Git
Docker
Kubernetes
AWS
[Typescript] keyof / typeof / indexed access type / mapped types
language
typescript

[Typescript] keyof / typeof / indexed access type / mapped types

Jay Kim
Jay Kim 05 Feb 2024
[Typescript] 제네릭 타입 [Typescript] Index signature

Table of Contents

  • keyof
  • typeof
  • Indexed Access Type
  • Mapped Types

keyof

  • 객체타입의 키(key)값으로 이루어진 유니언 타입을 반환한다
interface Car {
    brand: string
    color: string
}

type CarPropertiesType = keyof Car // 'brand' | 'color'

const brand: CarPropertiesType = 'brand'
const color: CarPropertiesType = 'color'

typeof

  • 피연산자의 타입을 반환한다
const car: Car = { brand: 'Tesla', color: 'red' }

typeof car // 'object'

Indexed Access Type

  • 다른 객체 타입의 프로퍼티의 타입을 타입화하는 것을 말한다
type Person = { age: number; name: string; alive: boolean };

type Age = Person["age"]; // number
type AgeOrName = Person["age"| "name"]; // number | string
type KeyofPerson = Person[keyof Person]; // number | string | boolean

Mapped Types

  • 다른 타입의 속성을 순회하여 새로운 타입을 만드는 것을 말한다
  • 맵드 타입에서 in은 마치 자바스크립트의 for in 문법과 유사하게 동작한다

  • 아래의 예제를 보면, key에는 제네릭 타입 T의 키를 순회하며 name, age, address가 하나씩 들어간다
type User = {
    name: string
    age: number
    address: string
}

type UpdateUser<T> = {
    [key in keyof T]?: T[key]
}

let updateBody: UpdateUser<User>

updateBody = {}
updateBody = { name: 'Park' }
  • 위에서는 옵셔널 연산자(?)를 붙여줌으로써 새로운 UpdateUser 라는 새로운 타입을 정의했다
  • 반대로 옵셔널 연산자를 제거할 수도 있다. -를 붙여주면 해당 연산자를 제거한다는 의미가 된다
type RequiredUser<T> = {
    [key in keyof T]-?: T[key]
}

let requiredUser: RequiredUser<UpdateUser<User>>

requiredUser = { name: 'Lee', age: 24, address: 'LA' }
[Typescript] 제네릭 타입 [Typescript] Index signature

You may also like

See all typescript
06 Feb 2024 [Typescript] 타입 심화편(2): 객체
language
typescript

[Typescript] 타입 심화편(2): 객체

06 Feb 2024 [Typescript] 타입 심화편(1): 함수
language
typescript

[Typescript] 타입 심화편(1): 함수

06 Feb 2024 [Typescript] 데코레이터
language
typescript

[Typescript] 데코레이터

Jay Kim

Jay Kim

Web development, data engineering for human for the Earth. I share posts, free resources and inspiration.

Rest
Lifestyle
Hobby
Hobby
Hobby
Hobby
2025 © Jay's Cookbook. Crafted & Designed by Artem Sheludko.