Value Objects – DDD with TypeScript
This seems like overengineering and a needless application of OOP. The author presents ideas like service classes or static factory methods as if they are self-evident, but I don&#x27;t understand what would make someone write TypeScript code this way besides too much Java experience. The final solution has the value of the “name” field nested multiple objects deep!<p>How about this code instead:<p><pre><code>    export interface User {
      readonly name: string;
    }

    export namespace User {
      export function validate(user: User) {
        &#x2F;&#x2F; In &quot;strict&quot; TS, `name` is required at compile time to not be null or undefined.
        if (name.length &lt; 2 || name.length &gt; 100) {
          throw new Error(&#x27;User must be greater than 2 chars and less than 100.&#x27;);
        }
      }
    }
</code></pre>
Unlike the OOP solution, there&#x27;s no guarantee that any given `User` is valid unless you check it with `User.validate`, but there should be minimal places if not one single place where users are actually validated, such as where they are saved to the database or backend.