3 category: JavaScript libraries
6 TypeScript is just like ES2015 with type-checking. All ES2015 (classes, etc) should work.
21 string[] /* or Array<string> */
22 [string, number] /* tuple */
24 string | null | undefined /* union */
26 never /* unreachable */
30 enum Color {Red, Green, Blue = 4}
31 let c: Color = Color.Green
38 let isDone: boolean = false
42 function add (a: number, b: number): number {
46 // Return type is optional
47 function add (a: number, b: number) { ... }
54 let len: number = (input as string).length
55 let len: number = (<string> input).length /* not allowed in JSX */
60 function object(this: {a: number, b: number}, a: number, b: number) {
66 // this is used only for type declaration
68 // a has type {a: number, b: number}
77 function printLabel (options: { label: string }) {
78 console.log(options.label)
82 function getUser (): { name: string; age?: number } {
89 interface LabelOptions {
93 function printLabel(options: LabelOptions) { ... }
96 ### Optional properties
109 readonly name: string
117 [key: string]: Object[]
124 type Name = string | string[]
130 interface User { ... }
132 function getUser(callback: (user: User) => any) { callback({...}) }
134 getUser(function (user: User) { ... })
144 constructor(x: number, y: number) {
156 class Point3D extends Point {...}
158 interface Colored {...}
160 class Pixel extends Point implements Colored {...}
163 #### Short fields initialisation
167 static instances = 0;
175 #### Fields which do not require initialisation
178 public someUselessValue!: number;
188 constructor(message: T) {
189 this.greeting = message
193 let greeter = new Greeter<string>('Hello, world')
199 export interface User { ... }
212 type Walls = Building['room']['walls']; // string[]