Announcing TypeScript 1.4
blogs.msdn.com
blogs.msdn.com
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.
Little oddnesses here and there in DefinitelyTyped d.ts choices.
Overall though typescript feels like first class javascript+a much better future javascript.
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 {}Luckily TypeScript helps out quite a lot and basically for free. Converting an existing JavaScript project to "naive" TypeScript takes hardly any effort, and as the project evolves it is easy to add type annotations and interfaces at any pace. Once a module is properly typed it (almost) stops breaking at run-time, yay! It's also very clear what will be executing in the browser. Using a cross-compiled language that follows the principle of least astonishment is a real treat.
Obviously it can't prevent compile-time errors, generate unit tests, or build a good application for you, but you do get quite a lot of help with the problems it can solve.
e.g. instead of
"hello\n" +
"world";
use
`hello
world`;
One nice thing about the template strings in TypeScript is you can use those and still compile to ES3/ES5 for today's browsers.
Who needs javascript if you have dart? Who needs dart if you have go? Who needs go if you have erlang? Who needs erlang if you have c? Who needs c if you have assembly? Who needs assembly if you have binary code? Who needs binary code if you can just control electric signals by manually plugging and unplugging a cable to the wall?
Apparently all you need to build a good website is a power cord and some patience!
* Non-nullable types: https://github.com/Microsoft/TypeScript/issues/185
* Sum types: https://github.com/Microsoft/TypeScript/issues/186
The short story is that there's some interest in both features, but there are challenges that someone needs to propose a solution to.
TypeScript now supports using ‘let’ and ‘const’ in addition
to ‘var’. These currently require the ES6 output mode, but
we’re are investigating relaxing this restriction in future
versions.
Would be quite nice to be able to use 'let', and just use variable renaming to generate ES5 code with unique names in the case where scopes overlap. I guess it might be coming? function f(val:any): val is SomeType {
do checking and return boolean
}
and will be most likely available in 1.5.Advanced pattern matching, monads, etc. might be out of scope for TS, since it doesn't want to have major syntactical additions from regular JS.
Ie whats in typescript es6 output that will have issues?
"In addition to the type system improvements, one of the main goals for the upcoming TypeScript 2.0 release is to fully support the ECMAScript 6 standard. With TypeScript 1.4, we take another step towards this goal. In this release, we’ve added a new ES6 output mode, support for let and const, and support for ES6 template strings."
At this point we really need to see ES6/7 and TypeScript + Flow "gene-culture co-evolution". As I said at the last TC39 meeting on the topic, big de-facto standard wins trump rushed de-jure standards any day.
/be
http://en.wikipedia.org/wiki/Dual_inheritance_theory.
The analogy is
* core JS standard = genes, DNA in full (mitochondrial [asm.js? :-P] and nuclear [dynamically typed JS]);
* Flow, TS, TS* and other research = culture.
Obviously it's a loose analogy, not an identity. There's no "Red Queen" (sex, sexual selection), but we do definitely see selection over time via developer adoption, what wins on performance and ergonomics, what feeds back from the ecosystem into the core standard APIs.
Also even a few new special forms: generators in ES6 (prototyped in ES4) were inspired by many experiments; mainly they constitute introgression from Python 2.5+.
JS as an evolutionary kernel (http://www.cc.gatech.edu/~sakhshab/evoarch-extended.pdf) consists of almost totally conserved material (always extended, backward compatibility required for adoption by developers and competing browsers), only slowly extended, while diverse languages and frameworks evolve above and below -- Node.js 14 years after I put JS in Netscape 2, e.g.
Feedback goes both ways, especially in the post-ES3 era where Firefox brought the browser market back to life. See ECMA-262 later editions (the Harmony era) rolling up de-facto standards such as Array map/reduce/forEach/etc.
The same feedback could and should happen with a standardized type system of some sort. It won't happen via design by committee, or picking one winner too soon.
Type system implementors and the JS stewards must communicate well for this to win. It's looking good so far, on Ecma TC39: JQuery, Facebook, Netflix, PayPal (Ebay originally, and again), Twitter all represented along with Apple, Google, Microsoft, and Mozilla. Also academic researchers from various universities, all of whom love type systems and theory :-).
/be
Problem is they are not available to users. I dont care what the JIT does,I want my IDE to help me write correct code,that's the goal of Typescript.
Maybe that is the stable?