Hacker News new | past | comments | ask | show | jobs | submit login

Agreed. We migrated most of the ES6 codebase to Typescript, and it feels great.

We have an angular.js app that makes REST calls. The Java backend has DTOs (data transfer objects) that are mapped by Jackson to json, and after migrating to Typescript, we also run the DTOs through compile-phase that generates corresponding typescript interfaces.

i.e.

in Java:

  public class PersonInfo { // the dto for person
     public final String name;
     public final PersonId id; // we have a class for each id type
     public PersonInfo(...) { ... }
  }

  @RestController
  public PersonController {
    
    @RequestMapping("/get-person-info")
    public PersonInfo getPersonInfo(@RequestParam int id) {
      // dummy example
      return new PersonInfo("Peter Jackson", id);
    }  
  }
In the java->typescript generator we add the controllers for which we need the rest API definitions to be generated as typescript interfaces:

   generator.addClassesFromController(PersonController.class);
And when the generator is run, a typescript file is generated with following definitions:

    export interface PersonInfo {
        name: string
        id: PersonId
    }

    interface PersonId {}



What tool do you use to emit the typescript interfaces? I looked around for one of these a few months ago but didn't find anything at the time.


It's custom made. We are extracting it from our current project, anyway, so I'll ask the dev responsible if he would release it as open source project.


You can cheat by using Java reflection on the POJO, then it is just a "relatively" simple matter of printing strings.




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: