[Typescript] 커스텀 Type 검사하는 함수 만들기

빠샤빠샤·2022년 10월 13일
0

TypeScript

목록 보기
1/1

기본적인 data type이 아닌 내가 만든 interface나 type으로 구성된 값인지 확인하고 싶은 경우가 있다.


예를 들어, Person이라는 interface를 만들었다고 했을때
interface Person {
	name: string,
    age: number
}

💡 아래와 같은 함수를 만들어 사용하자.

function isTypePerson(param: any): param is Person {
	return true;
}

isTypePerson 함수에 전달하는 param의 type이 Person이면 true를 아니면 undefined를 되돌려준다.
profile
UI/UX Designer & Frontend Developer

0개의 댓글