TypeScript
Back in 1996
SJavaScript:
"The Good Parts"
Douglas Crockford
ecmascript
TypeScript
Anders Hejlsberg
I invented
Turbo Pascal,
Delphi and C#
TypeScript in a Nutshell
- Typed superset of JavaScript
- Compiles to pure JavaScript
- Type Inference
- ES6 + ES7 features today
- Interfaces, Generics,...
Types
function sum(s) {
return s + s;
}
function square(x) {
return x * x;
}
JavaScript
function sum(s: string) : string {
return s + s;
}
function square(x: number) : number {
return x * x;
}
TypeScript
Type Inference
Syntax
interface Post {
text: string;
title?: string;
author: string;
type: 'post' | 'comment';
}
@Component({
selector: 'post',
template: '<div>My post</div>'
})
class PostComponent {
...
}
let myPost: Post = { }
// Type '{}' is not assignable to type 'Post': Property 'text' is missing
.d.ts Files
declare module Backbone {
export class Model {
constructor (attr? , opts? );
get(name: string): any;
set(name: string, val: any): void;
set(obj: any): void;
save(attr? , opts? ): void;
destroy(): void;
bind(ev: string, f: Function, ctx?: any): void;
toJSON(): any;
}
export class Collection<T> {
...
}
}
.d.ts Files
npm install @types/backbone --save-dev
// package.json
"dependencies": {
"backbone": "^1.3.33"
},
"devDependencies": {
"@types/backbone": "^1.3.33"
}
npm install @angular/core --save
// node_modules/@types
backbone
jquery
underscore
Demo time!
Should I start using TypeScript?
- TypeScript is here to stay
- .d.ts files :-/
- Static Typing is overrated
- Overhead for tooling
- Overuse of any
Should I start using TypeScript?
- TS won't have your back at runtime
- EcmaScript standard may change
- Makes it harder for newbies to learn JavaScript
- IDE support
Should I start using TypeScript?
- It's fun to write TS :)
- Refactoring codebase in a large project
- Open Source
- Roadmap
Thanks!
Links
TypeScript
By Michael Müller
TypeScript
- 1,070