개념
unknown, any 타입을 지정하기 애매할 때 사용
unknown
타입이 뭔지 모르겠으니 개발자가 타입을 정확히 지정주어야 한다.
typeof, instanceof 등 타입을 확인하여 적절한 타입 주입시켜 사용한다.
Example
code reference: https://docs.nestjs.com/exception-filters#catch-everything
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
constructor(private readonly httpAdapterHost: HttpAdapterHost) {}
catch(exception: unknown, host: ArgumentsHost): void {
const { httpAdapter } = this.httpAdapterHost;
const ctx = host.switchToHttp();
const httpStatus =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const responseBody = {
statusCode: httpStatus,
timestamp: new Date().toISOString(),
path: httpAdapter.getRequestUrl(ctx.getRequest()),
};
httpAdapter.reply(ctx.getResponse(), responseBody, httpStatus);
}
}
any
어떠한 타입이 될 수 있으니 자유롭게 사용 가능
참고
https://docs.nestjs.com/exception-filters#catch-everything
https://simsimjae.tistory.com/464
Documentation | NestJS - A progressive Node.js framework
Nest is a framework for building efficient, scalable Node.js server-side applications. It uses progressive JavaScript, is built with TypeScript and combines elements of OOP (Object Oriented Programming), FP (Functional Programming), and FRP (Functional Rea
docs.nestjs.com
'IT > typescript' 카테고리의 다른 글
[Typescript] enum vs union (0) | 2024.02.05 |
---|---|
TS - 5.0 New features (0) | 2023.07.01 |
TS - never (0) | 2023.06.27 |
TS - Extract<T, U> (0) | 2023.06.27 |
TS - Exclude<T, U> (0) | 2023.06.27 |