Project : 오브젝트 타입변환

최문길·2024년 1월 26일
0

project

목록 보기
7/17
post-thumbnail

문제

supabase sales 테이블 type을 인자값의 타입으로 지정 했는데, 내가 원하는 key 값이 string | null 이다.

현재 받는 인자값에 product_name 은 우리 팀의 convention 에서 무조건 product_nameorder_type

sales table에 추가 하기로 해서 null 이 될 수 없는 상황이다.

그런데 지금 supabase에서 제공하는 type에는 null 이 있는 상황이므로 데이터를 가공 할 때 type Error 가 띄워지는 상황




여기서 type을 내가 직접 다시 노가다로

sub : {
  id:string;
  order_type: string | null;
  product_category: string | null;
  product_ea: number;
  product_name: string | null ; 
  product_price : number; 
  sales_date : string | null;
  store_id : string ;

" 이렇게 해도 되겠지만, 방법이 없을까 ?? " 고민하면서도 ,

방향성

Generic 이 계속 떠오르게 되어서 ( 뭔가 직감적으로 계속 ... ) Generic을 가지고 생각해보았다.

해결

type TypeChanger<T> = {
  [key in keyof T] : string
}

type FormattedSalesType = TypeChanger<Tables<'sales'>>

0개의 댓글