diff options
author | bezigon <badabsinthe@mail.ru> | 2017-05-25 18:35:08 +0300 |
---|---|---|
committer | ven <vendethiel@hotmail.fr> | 2017-05-25 15:35:08 +0000 |
commit | 1ac2f69479caef3218e3bf8ab6d88351af9ed3eb (patch) | |
tree | cea8d1204f6610f1493fc8528a9276f5b4e705dd /typescript.html.markdown | |
parent | 66c4b1c01a26fed2b55ff50b4aba8b2c5eb9061c (diff) |
Update typescript.html.markdown (#2738)
https://github.com/adambard/learnxinyminutes-docs/issues/2737
Diffstat (limited to 'typescript.html.markdown')
-rw-r--r-- | typescript.html.markdown | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/typescript.html.markdown b/typescript.html.markdown index d3ca0786..ef37182d 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -15,23 +15,32 @@ To test TypeScript's compiler, head to the [Playground] (http://www.typescriptla ```ts // There are 3 basic types in TypeScript -var isDone: boolean = false; -var lines: number = 42; -var name: string = "Anders"; +let isDone: boolean = false; +let lines: number = 42; +let name: string = "Anders"; + +// But you can omit the type annotation if the variables are derived from explicit literals +let isDone = false; +let lines = 42; +let name = "Anders"; // When it's impossible to know, there is the "Any" type -var notSure: any = 4; +let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean +// Use const keyword for constant variables +const numLivesForCat = 9; +numLivesForCat = 1; // Error + // For collections, there are typed arrays and generic arrays -var list: number[] = [1, 2, 3]; +let list: number[] = [1, 2, 3]; // Alternatively, using the generic array type -var list: Array<number> = [1, 2, 3]; +let list: Array<number> = [1, 2, 3]; // For enumerations: -enum Color {Red, Green, Blue}; -var c: Color = Color.Green; +enum Color { Red, Green, Blue }; +let c: Color = Color.Green; // Lastly, "void" is used in the special case of a function returning nothing function bigHorribleAlert(): void { @@ -43,16 +52,16 @@ function bigHorribleAlert(): void { // The following are equivalent, the same signature will be infered by the // compiler, and same JavaScript will be emitted -var f1 = function(i: number): number { return i * i; } +let f1 = function (i: number): number { return i * i; } // Return type inferred -var f2 = function(i: number) { return i * i; } +let f2 = function (i: number) { return i * i; } // "Fat arrow" syntax -var f3 = (i: number): number => { return i * i; } +let f3 = (i: number): number => { return i * i; } // "Fat arrow" syntax with return type inferred -var f4 = (i: number) => { return i * i; } +let f4 = (i: number) => { return i * i; } // "Fat arrow" syntax with return type inferred, braceless means no return // keyword needed -var f5 = (i: number) => i * i; +let f5 = (i: number) => i * i; // Interfaces are structural, anything that has the properties is compliant with // the interface @@ -66,19 +75,19 @@ interface Person { // Object that implements the "Person" interface // Can be treated as a Person since it has the name and move properties -var p: Person = { name: "Bobby", move: () => {} }; +let p: Person = { name: "Bobby", move: () => { } }; // Objects that have the optional property: -var validPerson: Person = { name: "Bobby", age: 42, move: () => {} }; +let validPerson: Person = { name: "Bobby", age: 42, move: () => { } }; // Is not a person because age is not a number -var invalidPerson: Person = { name: "Bobby", age: true }; +let invalidPerson: Person = { name: "Bobby", age: true }; // Interfaces can also describe a function type interface SearchFunc { (source: string, subString: string): boolean; } // Only the parameters' types are important, names are not important. -var mySearch: SearchFunc; -mySearch = function(src: string, sub: string) { +let mySearch: SearchFunc; +mySearch = function (src: string, sub: string) { return src.search(sub) != -1; } @@ -104,8 +113,8 @@ class Point { static origin = new Point(0, 0); } -var p1 = new Point(10 ,20); -var p2 = new Point(25); //y will be 0 +let p1 = new Point(10, 20); +let p2 = new Point(25); //y will be 0 // Inheritance class Point3D extends Point { @@ -115,7 +124,7 @@ class Point3D extends Point { // Overwrite dist() { - var d = super.dist(); + let d = super.dist(); return Math.sqrt(d * d + this.z * this.z); } } @@ -131,12 +140,12 @@ module Geometry { } } -var s1 = new Geometry.Square(5); +let s1 = new Geometry.Square(5); // Local alias for referencing a module import G = Geometry; -var s2 = new G.Square(10); +let s2 = new G.Square(10); // Generics // Classes @@ -152,21 +161,21 @@ interface Pair<T> { } // And functions -var pairToTuple = function<T>(p: Pair<T>) { +let pairToTuple = function <T>(p: Pair<T>) { return new Tuple(p.item1, p.item2); }; -var tuple = pairToTuple({ item1:"hello", item2:"world"}); +let tuple = pairToTuple({ item1: "hello", item2: "world" }); // Including references to a definition file: /// <reference path="jquery.d.ts" /> // Template Strings (strings that use backticks) // String Interpolation with Template Strings -var name = 'Tyrone'; -var greeting = `Hi ${name}, how are you?` +let name = 'Tyrone'; +let greeting = `Hi ${name}, how are you?` // Multiline Strings with Template Strings -var multiline = `This is an example +let multiline = `This is an example of a multiline string`; ``` |