Outline

@nestia/core
export function TypedParam(name: string): ParameterDecorator;

Type safe path parameter decorator.

@TypedParam() is a decorator parsing path parameter.

It’s almost same with original @Param() function of NestJS, however, @TypedParam() is more type safe.

As @TypedParam() can anlayze source code in the compilation level, it can specify parameter type by itself. Also, while NestJS cannot distinguish nullable type and consider every parameter value as a string type, @TypedParam() can do it. Furthermore, @TypedParam() can validate special types like "uuid" or "date".

Let’s read below example code, and see how @TypedParam() works.

⚠️

@TypedParam() is not essential for Swagger Documents or SDK Library building.

Therefore, it is not a matter to use @TypedParam() or @Param() of the original NestJS.

How to use

ParametersController.ts
import { TypedParam } from "@nestia/core";
import { Controller, Get } from "@nestjs/common";
import { tags } from "typia";
 
@Controller("parameters")
export class ParametersController {
  @Get("uint32/:value")
  public async uint32(
    @TypedParam("value") value: (number & tags.Type<"uint32">) | null,
  ): Promise<(number & tags.Type<"uint32">) | null> {
    return value;
  }
 
  @Get("string/:value")
  public async string(
    @TypedParam("value") value: string
  ): Promise<string> {
    return value;
  }
 
  @Get("uuid/:value")
  public async uuid(
    @TypedParam("value") value: string & tags.Format<"uuid">,
  ): Promise<string> {
    return value;
  }
}

Just call @TypedParam() function on the path paremeter, that’s all.

If you want to special parameter type like "uint32" or "uuid", utilize type tags of typia.

When wrong typed value comes, 400 bad request error would be thrown.

Restriction

@TypedParam() allows only atomic type.

  • boolean
  • number
  • string
  • bigint

Also, @TypedParam() allows nullable like number | null, but undefindable type is not.

  • number | null is allowed
  • string | undefined is prohibited

If you violate above condition, and try to declare object or union type, compilation error would be occured:

Error on nestia.core.TypedParam(): only atomic type is allowed