Performance: TypeScript-first philosophy
This affects the development speed and runtime type safety, indirectly influencing bundle size and error detection before runtime.
import { ApiProperty } from '@nestjs/swagger'; class User { @ApiProperty() id: number; @ApiProperty() name: string; @ApiProperty() email: string; }
class User { id: any; name: any; email: any; } // No TypeScript types or decorators used
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No TypeScript types | N/A | N/A | N/A | [!] OK but risky |
| TypeScript-first with decorators | N/A | N/A | N/A | [OK] Good for development |
TypeScript-first philosophy in NestJS?Promise<User>.any or string.async findUser(id: number): Promise {
const user = await this.userRepository.findOneBy({ id });
return user ?? null;
}user ?? null, which means if user is nullish, return null.findOneBy returns undefined, the expression returns null explicitly.@Get(':id')
async getUser(@Param('id') id: string): Promise<User> {
return this.userService.findUser(id);
}findUser can return null when user not found, the controller must reflect that in its return type.Promise if user might not exist. correctly points out the return type should allow null. The parameter id should be typed as number not string. is not always true because id can be string if parsed later.name string and an optional age number using TypeScript-first philosophy. Which of these is the best TypeScript definition?name must be required (no question mark), age optional (with question mark).name as required and age as optional in a class, which is standard for NestJS DTOs.Pass the test and this pattern moves unknown