3 ms·
Very interesting. The Apache 2.0 Licence is comforting, makes it worth having a look. The docs are here for anyone interested in the spec: http://typescript.co
by scanr 14y ago
Very interesting. The Apache 2.0 Licence is comforting, makes it worth having a look. The docs are here for anyone interested in the spec:
http://typescript.codeplex.com/documentation http://typescript.codeplex.com/documentation
I like what they've done. Nothing too revolutionary, mostly adding static typing to Javascript without straying to far from the existing (or future) language or increasing the noise significantly.
Here's some highlights:
* Type inference
function f() {
return "Hello World!";
}
Will do the right thing
* Explicit Typing
function f(s: string) {
return s;
}
* 'Ambient Types'
To facilitate integration into existing JS libraries or the DOM, TypeScript lets you specify 'placeholders' for variables / classes that the runtime would expect to see e.g.
declare var document;
Almost a kind of dependency injection but also a neat way to manage talking to existing code.
* Structural Typing
* Classes and Interfaces
interface BankAccount {
balance: number;
deposit(credit: number): number;
}
class CheckingAccount extends BankAccount {
constructor(balance: number) {
super(balance);
}
writeCheck(debit: number) {
this.balance -= debit;
}
}
* Modules
module M {
var s = "hello";
export function f() {
return s;
}
}
* Arrow function expressions
(x) => { return Math.sin(x); }
- euroclydon 14y agoAre you actually implying with the above example code that you can call a undefined "super" constructor on an interface and pass it the "balance" variable, which the interface's non-existent constructor would presumably match by name?
- scanr 14y agoNope. I probably should have used a different example for the interface. The examples are from the full spec where they progress a little more gradually from a BankAccount interface to a BankAccount class to a CheckingAccount subclass.
- zokier 14y agoI haven't read the docs so I may be completely off. But that "super" syntax could be similar to "brace initialization" in C.
- bmuon 14y agoI don't think `super` can be invoked as a function yet. I think it's only a reference to the superclass' prototype.