HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🚀
개발 노트
/
🏦
Utility Types
🏦

Utility Types

 
 

ReturnType<Type>

함수 Type의 반환 타입으로 구성된 타입을 생성한다.
type T0 = ReturnType<() => string>; // type T0 = string type T1 = ReturnType<(s: string) => void>; // type T1 = void declare function f1(): { a: number; b: string }; type T3 = ReturnType<typeof f1>; // type T3 = { a: number; b: string; }
setTimeout의 반환 타입을 알아내기 위해 사용하였다.
 
const timerId = useRef<ReturnType<typeof setTimeout>>(); // timerId.current: MutableRefObject<NodeJS.Timeout | undefined> timerId.current = setTimeout(() => { handler(e); }, ms);
아래와 같이 window를 붙여서 브라우저 환경임을 나타내는 방법도 있다. window.setTimeout의 반환 타입은 number이다.
 
const timerId = useRef<number>(); // timerId.current: number | undefined timerId.current = window.setTimeout(() => { handler(e); }, ms);
 
 
Documentation - Utility Types
TypeScript는 일반적인 타입 변환을 쉽게 하기 위해서 몇 가지 유틸리티 타입을 제공합니다. 이러한 유틸리티는 전역으로 사용 가능합니다. Type 집합의 모든 프로퍼티를 선택적으로 타입을 생성합니다. 이 유틸리티는 주어진 타입의 모든 하위 타입 집합을 나타내는 타입을 반환합니다. Type 집합의 모든 프로퍼티를 필수로 설정한 타입을 생성합니다. 의 반대입니다.
Documentation - Utility Types
https://www.typescriptlang.org/ko/docs/handbook/utility-types.html
Documentation - Utility Types
싸이월드
setTimeout(handler: (...args: any[]) => void, timeout: number): number; 그러나 컴파일러는 이를 대신 노드 구현으로 해결하여 NodeJS.Timer를 반환합니다. setTimeout(callback: (...args: any[]) => void, ms: number, ...args: any[]): NodeJS.Timer; 이 코드는 노드에서 실행되지 않지만 노드 유형은 다른 것에 대한 종속성으로 가져옵니다(무엇인지 확실하지 않음). TS2322: '타이머' 유형은 '숫자' 유형에 할당할 수 없습니다.
싸이월드
https://cyworld.tistory.com/3253
싸이월드