class-validator icon indicating copy to clipboard operation
class-validator copied to clipboard

feature: treat some function as type assertions

Open Motii1 opened this issue 3 years ago • 0 comments

Description

Sometimes I need to use functions directly instead of decorators, e.g. isUUID. Let's imagine the case in typescript where you validate big csv with streams row by row, e.g.

if (!isUUID(id) || seenIds.has(id as string)) {
      throw new InvalidFileStructureException();
 }
 seenIds.add(id as string); // seenIds is of type Set<string>

It would be really convenient if we would be able to replace the line

seenIds.add(id as string);

with

seenIds.add(id)

because it is obvious that isUUID checks if something is a string of correct format.

Proposed solution

Treat some functions like mentioned isUUID as type assertions when it is obvious when the function assert some type, e.g. string

Motii1 avatar Feb 24 '23 11:02 Motii1