본문 바로가기
IT/React

제너릭 타입

by 골든크랩 2022. 11. 1.
728x90
반응형

 

예)

const arrayLength = (array) => array.length

 

Generic으로 바꾸면...

const arrayLength = (array: T[]) => array.length

 

컴파일러가 T가 타입 변수(type variable) 라고 알려줘야 함.

export const arrayLength = <T>(array: T[]) number => array.length

export const isEmpty = <T>(array: T[]) : boolean => arrayLength<T>(array) == 0

 

 

타입 추론

const identity = <T>(n: T) : T => n

console.log(
    identity<boolean>(true),
    identity(true)
)

728x90
반응형

'IT > React' 카테고리의 다른 글

React-boilerplate-cra-template 이란?  (0) 2023.01.07
React Craco 살펴보기  (0) 2023.01.06
Could not resolve dependecy  (0) 2022.10.24
useRecoilState 사용법  (0) 2022.09.30
useLocation 기능 - 현재 URL 정보를 가져온다.  (0) 2022.09.30

댓글