TypeScript is a typed superset of JavaScript that introduces static typing and classes to JavaScript. It adds a compiler that validates code based on static types, interfaces, modules, and other features. Key features include declared types for variables and function parameters/returns, classes with inheritance and static properties, modules to organize code, and interfaces with structural typing. External type declarations bring these benefits to existing JavaScript libraries without changing the code.
7. STATIC PROPERTIES
class Customer extends Person {
private static sequence: number = 1;
public static nextNumber(): number {
return Customer.sequence++;
}
private id: number;
constructor(name: string, age: number) {
super(name, age);
this.id = Customer.nextNumber();
}
toString(): string {
return super.toString() + ", id: " + this.id;
}
}
var c1: Customer = new Customer("Oma", 88);
console.log(c1.toString());
8. MODULES
module CustomerModule {
class Person {
// ...
}
export class Customer extends Person {
// ...
}
}
// new CustomerModule.Person(...); // => undefined
new CustomerModule.Customer(...); // => cool
9. STRUCTURAL TYPING WITH INTERFACES
interface HasName {
name: string;
}
var olli: HasName = {name: 'Olli'};
olli.name; // good
olli.age; // not good, as not declared
olli = {name: 'Olli', age: 100};
olli.age; // still not good, as not declared
10. EXTERNAL DECLARATIONS
Adds tooling and type checking to existing
JavaScript libraries
Declarations disappear when compiled
TypeScript compiler comes with external
declarations for core and DOM libraries
(lib.d.ts)
Work in progress for a lot of other libraries
13. TOOLS
tsc: Command Line Tool for Compiler
WebStorm / Intellij IDEA
Visual Studio (Express for Web)
Syntax highlighting for other text editors (Sublime
Text, Emacs, Vim)
15. RESOURCES
These Slides on GitHub:
Code Samples on GitHub:
https://github.com/DJCordhose/typescript-hh-js
https://github.com/DJCordhose/typescript-
sandbox
17. Collection of external declaration files
Syntax files for other editors
Great introductory talk by Anders Hejlsberg
TypeScript with AngularJS
AngularJS + TypeScript : Controllers, Best
Practice
Grunt Plugin for TypeScript
Version 0.9 with Generics
heise.de on TypeScript
Experience report Generics
TypeScript deep dive
19. TypeScript has the same semantics as JavaScript
Declared types bring you to the next level of (IDE)
tooling
Analyzing Code
Refactoring
Code completion
Syntactic sugar for classes, inheritance, and
modules is nice
All added features aligned to ECMAScript 6
Interfaces and external declarations add the
benefits of TypeScript to pure JavaScript libraries