TypeScript is pretty amazing. In a few weeks, it turned our node project from an unruly mess (with tests) to a well organized and compact codebase with nice interfaces that formalize the various data types that flow around.
We just changed the extensions of all of our files to `ts` and added some type definition files (most from DefinitelyTyped) to our project; then started gradually adding types to the codebase. The compiler complains about type errors, but always generates JS from code that is still valid JS, so the project was fully operational during the migration.
Once that was done, moving code around, renaming things and changing entire interfaces became really easy.
The structural type system (basically formalized duck types) is a great fit for JS, and features such as generics ensure that the typesystem is expressive enough to model common functional code (no higher kinded types though, look into purescript for that).
Its not perfect (all types nullable by default; function arguments are bivariant wrt input/output params and some other weirdness here and there) but its a remarkable improvement over plain JS.
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:
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.
" a remarkable improvement over plain JS" - Agreed. Optional types for the win. Types when you want them but just make something <any> when you want to get up to your old js tricks. Dart gives you a first class experience of optional typing with things like checked mode at runtime but I think typescript gets the 80/20 by enhancing js in a very clever way.
We just changed the extensions of all of our files to `ts` and added some type definition files (most from DefinitelyTyped) to our project; then started gradually adding types to the codebase. The compiler complains about type errors, but always generates JS from code that is still valid JS, so the project was fully operational during the migration.
Once that was done, moving code around, renaming things and changing entire interfaces became really easy.
The structural type system (basically formalized duck types) is a great fit for JS, and features such as generics ensure that the typesystem is expressive enough to model common functional code (no higher kinded types though, look into purescript for that).
Its not perfect (all types nullable by default; function arguments are bivariant wrt input/output params and some other weirdness here and there) but its a remarkable improvement over plain JS.