Ensuring consistency between our front-end Typescript types and the corresponding types in our back-end source code

In our development process, we utilize Java for backend operations and TypeScript for frontend tasks, each residing in its own repository.

Lately, I've made a shift in the way we handle data exchange between server and client. Instead of sending individual properties like userName and age to the client, we now transmit "RestObjects" (such as UIRestUser) that provide more comprehensive information based on Java classes. These Java "RestClasses" are serialized to JSON and then parsed into objects by our JavaScript/TypeScript code. However, I'm concerned about potential gaps between our TypeScript and Java implementations. Currently, we create parallel interfaces/types in TypeScript to address this issue.

My goal is to define the source type just once. One idea I have is to automatically generate a JSON object representing the types `{userName: string, age: number}` or an actual object `{userName: "Doron", age: 32}` upon each Java commit. This generated JSON could help TypeScript infer the types accurately, possibly through the use of a commit hook or similar mechanism.

Many TypeScript users work with diverse languages on the server side. What strategies and tools can be employed to maintain alignment between types? I welcome any advice or recommendations regarding implementation details and useful tools/libraries for this purpose.

Answer №1

Consider integrating Conjure into your workflow. With Conjure, you can define your objects in Yaml and have Java/Typescript code generated automatically for you. For those using Gradle to build their backend, there's a convenient gradle-conjure plugin available to streamline the code generation process within your builds.

If Conjure doesn't entirely meet your needs, it still serves as a valuable reference point.

Please note: I am employed by Palantir and acknowledge the challenges of maintaining consistency between backend and frontend object types. Conjure was developed as a solution to address this issue.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to effectively set up Hibernate connection factory with a connection pool

I am looking to set up a connection pool using Hibernate. Below is my configuration file: <hibernate-configuration> <session-factory> <property name="hibernate.show_sql">true</property> <p ...

How did the bundle end up rewriting itself?

I am transferring the outcome back to the previous task public void sendDataBack() { Intent intent = new Intent(); Bundle data = new Bundle(); switch (sender) { case TARIF: data.putParcelableArrayList(TASK_MANAGER.PICK_TARI ...

The update() function in the ScoreView class is triggered, however, it fails to refresh the Label

public class ScoreView extends Container implements Observer { private Label livesLabel; private Label foodLevelLabel; public ScoreView() { // Display and include labels in the container this.setLayout(new FlowLayout(Component.CENTER)) ...

Mapping intricate entities to intricate DTOs using NestJS and TypeORM

Currently, I am using the class-transformer's plainToClass(entity, DTO) function to transform entities into DTO objects. In addition, I have implemented the transform.interceptor pattern as outlined in this source. I make use of @Expose() on propert ...

Utilizing a for loop in Java to multiply a series of numbers and return

I'm attempting to take a given number (n) and multiply it by every number less than itself. For example, if you enter 4, the calculation would be (1 x 2 x 3 x 4) = 24. However, my current code is only returning a result of 0. I have a similar function ...

Halting the run process of the Android application

My goal is to develop a BLE app that can carry out continuous scanning tasks at predefined intervals. The approach I have taken involves using two separate runnables that call each other in the following manner: private Runnable scan = new Runnable() { ...

Bringing in TypeScript definitions for gridster

After starting a new ionic project, I decided to include the gridster.js library by running npm install gridster and npm install @types/jquery.gridster in the root directory of my project. However, when trying to import the installed definitions, I encount ...

Challenges encountered while implementing generic types in TypeScript and React (including context provider, union types, and intersection

I have a fully functional example available at this link: The code is working properly, but TypeScript is showing some errors. Unfortunately, I've run out of ideas on how to make the code type safe. I've searched extensively for examples that ma ...

The development mode of NextJS is experiencing issues, however, the build and start commands are functioning normally

Just finished creating a brand new Next app (version: 12.0.7) using Typescript and Storybook. Everything seems to be working fine - I can successfully build and start the server. However, when I try to run dev mode and make a request, I encounter the follo ...

Is my Javascript experiencing a shortage of asyncIds? (Encountered RangeError in inspector_async_hook.js)

One issue that I frequently encounter while using async/await is the following error: RangeError: Value undefined out of range for undefined options property undefined at Set.add (<anonymous>) at AsyncHook.init (internal/inspector_async_hook ...

Issue with Selenium: Unable to input text into the designated input field

The click Event is functioning properly, however, the sendKeys event is not working as expected. Here is a snippet of my code: driver.findElement(By.id("radio-1-4")).click(); jse.executeScript("scroll(0, 500);"); System.out.println("Authority Filter") ...

Is there a way to access the tree from previous commits using the JGit API?

When trying to retrieve the parent commit tree for a given commit in order to compare changes, I noticed that the getTree() method on the parent RevCommit objects was always returning null. ObjectId lastCommitId = repository.resolve(Constants.HEAD); RevW ...

Having trouble exporting data to an excel file using Selenium WebDriver and Apache POI

Just getting started with Selenium Web automation, so go easy on me. I've put together a method to write an array's content to an excel sheet. No errors or exceptions are popping up, but the data isn't showing up in the excel sheet as expec ...

How can we effectively create selenium and testng automation scripts for conducting field level validation on a large quantity of fields, exceeding 100 in total?

Managing over 50 fields on one page can be challenging. I require field-level validation to ensure visibility, editability, mandatoriness, label spelling, text field limits, and field types such as dropdowns, numerics, alphanumerics, and checkboxes. What ...

What is causing the consistent occurrences of receiving false in Angular?

findUser(id:number):boolean{ var bool :boolean =false this.companyService.query().subscribe((result)=>{ for (let i = 0; i < result.json.length; i++) { try { if( id == result.json[i].user.id) ...

Is there a way to dynamically filter an array as I type?

When I use my solution, I have to enter "Back-End Developer" to filter results. Is there a way to show results just by typing "back" or "backend"? The filter doesn't seem to work if I don't include the "-". I think I need to incorporate some and ...

"Encountering a hiccup with AngularJS and Spring MVC: Error 400 - Bad

My attempt to post an object containing an ID (integer) and an array is resulting in a HTTP 400 Bad request response from the server side. Here's what I have so far... Java Bean Object for Request: public class GuardarVentaRequest { private Integer ...

What is the syntax for obtaining the return value using executeQuery() in Java?

Here is a code snippet I have: String sql = "UPDATE Players SET Losses=Losses+1 WHERE UUID='" + p.getUniqueId() + "';"; stmt.executeUpdate(sql); I'm looking to retrieve the current Losses value for a specific player, p. Any ideas on how t ...

What are the recommended ways to utilize AsyncTask for initializing SharedPreference and other general tasks effectively?

As a newcomer to native Android development, I recently added the following code within the onCreate method of my MainActivity class. Since I've never worked with AsyncTask before, I'm unsure if this is the best approach. Should I create a new A ...

C# form with embedded Jframe

I am currently facing an issue where I am attempting to incorporate a Java JFrame into a container within a C# form. The JFrame that I am using is also implementing an AppletStub, and I have utilized IKVM for interoperability. However, the problem arises w ...