0
0
NestJSframework~20 mins

TypeScript-first philosophy in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
NestJS TypeScript Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
How does NestJS use TypeScript types at runtime?

Consider a NestJS controller using TypeScript interfaces for request validation. What happens to these types when the app runs?

NestJS
interface CreateUserDto {
  name: string;
  age: number;
}

@Controller('users')
export class UsersController {
  @Post()
  create(@Body() createUserDto: CreateUserDto) {
    return `User ${createUserDto.name} created`;
  }
}
ATypeScript types remain in the compiled code and throw errors if data doesn't match.
BTypeScript interfaces are compiled into JavaScript classes and checked automatically at runtime.
CTypeScript types are removed at runtime; NestJS uses decorators and validation pipes to enforce data shape.
DNestJS converts TypeScript types into JSON schemas and validates requests without extra code.
Attempts:
2 left
💡 Hint

Think about what happens to TypeScript types after compilation.

📝 Syntax
intermediate
2:00remaining
Which NestJS syntax correctly uses TypeScript decorators for dependency injection?

Identify the correct way to inject a service into a NestJS controller using TypeScript decorators.

Aconstructor(userService: UserService) { this.userService = userService; }
Bconstructor(@Inject(UserService) userService) { this.userService = userService; }
Cconstructor(@Injectable() userService: UserService) {}
Dconstructor(private readonly userService: UserService) {}
Attempts:
2 left
💡 Hint

Remember how NestJS uses TypeScript's parameter properties for injection.

🔧 Debug
advanced
2:00remaining
Why does this NestJS service fail to inject the repository?

Given this service code, why does NestJS throw a runtime error about missing provider?

NestJS
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();
  }
}
AThe User entity is not included in the TypeOrmModule.forFeature() imports in the module.
BThe @InjectRepository decorator is used incorrectly; it should be @InjectableRepository.
CThe UsersService class is missing the @Controller decorator.
DThe Repository type is not imported from '@nestjs/typeorm'.
Attempts:
2 left
💡 Hint

Check the module setup for repository injection.

state_output
advanced
2:00remaining
What is the output of this NestJS controller method with TypeScript enums?

Given this enum and controller method, what will be returned when calling GET /status?

NestJS
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}`;
  }
}
A"Status is Active"
B"Status is active"
C"Status is 0"
D"Status is Status.Active"
Attempts:
2 left
💡 Hint

Remember how string enums work in TypeScript.

🧠 Conceptual
expert
3:00remaining
Why does NestJS recommend a TypeScript-first approach for DTOs and validation?

Choose the best explanation for why NestJS encourages defining DTOs with TypeScript classes and decorators instead of plain JavaScript objects.

ATypeScript classes with decorators enable automatic runtime validation and clear API contracts, improving maintainability and developer experience.
BPlain JavaScript objects are faster at runtime but harder to write, so NestJS avoids them.
CUsing TypeScript classes forces the app to compile slower but produces smaller bundles.
DNestJS requires TypeScript classes because JavaScript objects cannot be serialized to JSON.
Attempts:
2 left
💡 Hint

Think about how decorators and TypeScript types help beyond just typing.