TypeScript is billed as JavaScript with types, which is great and high tech and useful and all, but I really like that you can just write code that describes a class rather than writing code that meticulously constructs a class-like contraption.
With JavaScript, I’d say:
- There are many ways to do things.
- Some ways are obviously bad.
- Some ways are subtly bad.
- The good way isn’t obvious.
This is a class with some methods in a module in JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var twodspace = (function() { "use strict"; var Vec2 = function(x, y) { this.x = x; this.y = y; }; $.extend(Vec2.prototype, { equals: function(vec) { return this.x === vec.x && this.y === vec.y; } }); Vec2.distanceSq = function(a, b) { var dx = a.x - b.x, dy = a.y - b.y; return dx * dx + dy * dy; }; return { Vec2: Vec2 }; }()); |
This is the same in TypeScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
export class Vec2 { x: number y: number constructor(x: number, y: number) { this.x = x this.y = y } equals(obj: Vec2): boolean { return this.x === obj.x && this.y === obj.y } static distanceSq(a: Vec2, b: Vec2): number { const dx = a.x - b.x, dy = a.y - b.y return dx * dx + dy * dy } } |
Notice the words, such as “class” and “static”. These words can be written, then later read and understood by humans. I think this words-with-meanings concept is going to be a big hit.