Do you generate interfaces for your DTOs?
  v3.0 Posted at 26/07/2017 9:27 AM by Tiago Araujo

Inevitably, our well-engineered Angular ​application will need to send and receive data from a service of some sort – usually a Web API. A common mistake people make when doing this is using typescript’s built in any type for these services, which offers no type safety whatsoever.

Figure: Bad example - The "any" type is used as the DTO for this service. There is no type safety.

One step better is to manually create interfaces for the DTOs. This gives type safety, but still means a lot of manual, tedious work to generate the interfaces.

Figure: Better example - Manually coded interface ensures any object passed to the service is in the correct format

But this still doesn’t give safety over-the-wire – if the server side model changes, a developer has to remember to update it here, and hope that there are no typos.  This is also extra effort to perform something mindlessly repetitive – something a machine is much better at doing.  And we are programmers, right?

An example of such a generator is gulp-typescript-cs-poco ​, but there are many out there.

Figure: Good example - Writing a small bit of code means I don’t have to write a lot of code every time I change a DTO

Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:


    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.