TypeScript 합시다

[TypeScript][ch7][Generic] Generic Function

써치킴 2022. 2. 18. 02:09

함수의 타입만 선언하는 방식

// 1. type alias 이용
type HelloFunctionGeneric1 = <T>(message: T) => T;   // T를 받아서 T를 리턴

const helloFunction1:HelloFunctionGeneric1 = <T>(message: T): T => {
  return message;
}

// 2. interface 이용
interface HelloFunctionGeneric2 {
  <T>(message: T): T;
}

const helloFunction2: HelloFunctionGeneric2 = <T>(message: T): T => {
  return message;
}