How should I map my models to concrete classes?

This page summarizes the projects mentioned and recommended in the original post on /r/typescript

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • io-ts

    Runtime type system for IO decoding/encoding

    Then, with the way you're typing the Axios response I would go with a simple IUser interface if the endpoint and your classes share the same shape, but reduce the number of types you have since this is Ts and interfaces aren't enforced the way they would be in C#. Instead you can use a library like io-ts to validate the schema.

  • class-transformer

    Decorator-based transformation, serialization, and deserialization between objects and classes.

    The most straightforward answer is class-transformer. It's a library that has already seen widespread use in the Typescript community and is featured in the NestJS framework with a full array of features that meet your requirements.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

  • class-validator

    Decorator-based property validation for classes.

    On top of that, you want to keep in mind why some other commenters have said not to use classes. If you aren't getting any benefit from them, such as methods or getters and setters, it's more common to use objects in Ts but there's nothing inherently wrong with using classes as you can then combine them with additional libraries such as class-validator to bake a ton of functionality into your apps.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts