When dealing with validation in Node.js (TypeScript), which is the better pattern in general and why?:
Class with validation in the constructor:
Validation happens when the object is created, ensuring it's always valid.class DateRange { constructor(public startDate: Date, public endDate: Date) { if (startDate >= endDate) { throw new Error("startDate must be before endDate"); } }}
Type with a separate validation function:
Decouples validation from object creation but requires explicit validation.type DateRange = { startDate: Date; endDate: Date };function validateDateRange(range: DateRange): boolean { return range.startDate < range.endDate;}