---
title: "Discussion on \"How to achieve data consistency with TypeScript\" | Hashnode"
description: "Discussion on \"How to achieve data consistency with TypeScript\". One of the basics of the Object-Oriented programming is Encapsulation, which aims to never let our domain models enter an invalid state.\nThink about the peace of mind when persisting data or using it for any processing, if you can be absolutely sure ..."
url: "https://hashnode.com/posts/how-to-achieve-data-consistency-with-typescript/62201ae1aa0b9716581fc2db"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How to achieve data consistency with TypeScript](https://phillcode.io/how-to-achieve-data-consistency-with-typescript?utm_source=hashnode&utm_medium=feed)

One of the basics of the Object-Oriented programming is Encapsulation, which aims to never let our domain models enter an invalid state. Think about the peace of mind when persisting data or using it for any processing, if you can be absolutely sure ...

Mar 3, 2022 · 6 min read · phillcode.io  
Read the article

-   [#typescript](https://hashnode.com/tag/typescript)
-   [#validation](https://hashnode.com/tag/validation)
-   [#classes](https://hashnode.com/tag/classes)
-   [#object-oriented-programming](https://hashnode.com/tag/object-oriented-programming)
-   [#nodejs](https://hashnode.com/tag/nodejs)

## Written by

[Phillippe Santana](https://hashnode.com/@phillippelevidad)

@phillippelevidad · Curitiba, BR

  
[View profile](https://hashnode.com/@phillippelevidad)

CTO at INA

15 years as Software Engineer and Solutions Architect, I also have a strong experience in Leadership, Product Development and Project Management. Interested in: software architecture and development, excelent communication, people, personal development, playing guitar and travelling :)

## Responses 2

Markdown supported · ⌘↵ to post

[Aleksa](https://hashnode.com/@Lexpeartha)

[Mar 6, 2022](https://hashnode.com/posts/how-to-achieve-data-consistency-with-typescript/62201ae1aa0b9716581fc2db/comment/6224eecdecb2d35f765e4adf)

Great article, however, I don't think encapsulation is always the solution. Having lots of classes in React/Vue basically nullifies treeshaking, and unused parts of the classes will be bundled up in parts of the code where they are not actually used.

From frontend-only perspective, it is way more performant and intuitive to write hooks/composition functions instead, whether or not you use TS.

[Phillippe Santana](https://hashnode.com/@phillippelevidad)

[Mar 7, 2022](https://hashnode.com/posts/how-to-achieve-data-consistency-with-typescript/62201ae1aa0b9716581fc2db/comment/6224eecdecb2d35f765e4adf#reply-62267b2a282dd872a7a995d8)

Hi [Aleksa S.](https://hashnode.com/@Lexpeartha), thank you for your comment! This technique _may_ be more suitable for backend development.

If you **do** use this techinique in the frontend and apply the Single Responsibility Principle, you will not risk importing unused parts of a class, since the class has only one job, and the whole class will be used.

That said, hooks and composition functions **are** a form of encapsulation, since you're packaging logic in a way that is easy to use AND enforces correct usage at the same time (does not allow incorrect usage).

So, as long as the implementation details are hidden and the function/method/class enforces consistency (i.e. protects the data), you're doing encapsulation, just in different forms.

[Saurabh Mhatre](https://hashnode.com/@saurabhmhtr)

[Mar 4, 2022](https://hashnode.com/posts/how-to-achieve-data-consistency-with-typescript/62201ae1aa0b9716581fc2db/comment/62217c41d1601f3d285cc6fc)

Very well written article with good examples. Thanks for explaining these concepts 👍
