Consider a NestJS controller using TypeScript interfaces for request validation. What happens to these types when the app runs?
interface CreateUserDto {
name: string;
age: number;
}
@Controller('users')
export class UsersController {
@Post()
create(@Body() createUserDto: CreateUserDto) {
return `User ${createUserDto.name} created`;
}
}Think about what happens to TypeScript types after compilation.
TypeScript types and interfaces are erased during compilation. NestJS relies on decorators and validation pipes (like class-validator) to check data at runtime.
Identify the correct way to inject a service into a NestJS controller using TypeScript decorators.
Remember how NestJS uses TypeScript's parameter properties for injection.
Option D uses TypeScript's shorthand to declare and inject the service. Option D misses readonly and type declaration. Option D lacks decorators. Option D misuses @Injectable.
Given this service code, why does NestJS throw a runtime error about missing provider?
import { Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { User } from './user.entity'; @Injectable() export class UsersService { constructor(@InjectRepository(User) private userRepository: Repository<User>) {} findAll() { return this.userRepository.find(); } }
Check the module setup for repository injection.
To inject a repository, the entity must be registered in TypeOrmModule.forFeature() in the module. Without it, NestJS cannot provide the repository.
Given this enum and controller method, what will be returned when calling GET /status?
enum Status {
Active = 'active',
Inactive = 'inactive',
Pending = 'pending'
}
@Controller('status')
export class StatusController {
@Get()
getStatus(): string {
const currentStatus: Status = Status.Active;
return `Status is ${currentStatus}`;
}
}Remember how string enums work in TypeScript.
String enums assign string values. Accessing Status.Active returns 'active', so the returned string is 'Status is active'.
Choose the best explanation for why NestJS encourages defining DTOs with TypeScript classes and decorators instead of plain JavaScript objects.
Think about how decorators and TypeScript types help beyond just typing.
TypeScript classes combined with decorators allow NestJS to perform runtime validation (e.g., with class-validator), generate API docs, and provide clear, maintainable code. Plain objects lack these benefits.