타입스크립트 함수 메서드에 type alias 지정

버건디·2023년 1월 3일
0

타입스크립트

목록 보기
8/12
post-thumbnail
const 함수 = (a : string) : string => {
  return a;
}

보통 함수에 타입을 지정할때, 파라미터와 함수 리턴값에 각각 타입을 지정해주는데,

함수 자체에 타입을 지정해줄 수 있다.

type FuncType = (a : string) => number; // 리턴값이 없을 경우 {} 생략 가능

const 함수 :FuncType = (a) => {
  return 10;
}

함수에 type alias 를 지정해주고 그 타입을 사용하려면 함수 표현식으로 작성해주어야한다.

type CutZeroType = (a: string) => string;
type RemoveDashType = (a: string) => number;

const cutZero: CutZeroType = (a) => {
  return a;
};

const removeDash: RemoveDashType = (a) => {
  return 10;
};


cutZero('a');
removeDash('c');
profile
https://brgndy.me/ 로 옮기는 중입니다 :)

0개의 댓글