Update typescript.html.markdown (#2738)

https://github.com/adambard/learnxinyminutes-docs/issues/2737
This commit is contained in:
bezigon 2017-05-25 18:35:08 +03:00 committed by ven
parent 66c4b1c01a
commit 1ac2f69479

View File

@ -15,23 +15,32 @@ To test TypeScript's compiler, head to the [Playground] (http://www.typescriptla
```ts ```ts
// There are 3 basic types in TypeScript // There are 3 basic types in TypeScript
var isDone: boolean = false; let isDone: boolean = false;
var lines: number = 42; let lines: number = 42;
var name: string = "Anders"; 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 // 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 = "maybe a string instead";
notSure = false; // okay, definitely a boolean 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 // 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 // Alternatively, using the generic array type
var list: Array<number> = [1, 2, 3]; let list: Array<number> = [1, 2, 3];
// For enumerations: // For enumerations:
enum Color {Red, Green, Blue}; enum Color { Red, Green, Blue };
var c: Color = Color.Green; let c: Color = Color.Green;
// Lastly, "void" is used in the special case of a function returning nothing // Lastly, "void" is used in the special case of a function returning nothing
function bigHorribleAlert(): void { function bigHorribleAlert(): void {
@ -43,16 +52,16 @@ function bigHorribleAlert(): void {
// The following are equivalent, the same signature will be infered by the // The following are equivalent, the same signature will be infered by the
// compiler, and same JavaScript will be emitted // 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 // Return type inferred
var f2 = function(i: number) { return i * i; } let f2 = function (i: number) { return i * i; }
// "Fat arrow" syntax // "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 // "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 // "Fat arrow" syntax with return type inferred, braceless means no return
// keyword needed // 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 // Interfaces are structural, anything that has the properties is compliant with
// the interface // the interface
@ -66,19 +75,19 @@ interface Person {
// Object that implements the "Person" interface // Object that implements the "Person" interface
// Can be treated as a Person since it has the name and move properties // 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: // 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 // 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 // Interfaces can also describe a function type
interface SearchFunc { interface SearchFunc {
(source: string, subString: string): boolean; (source: string, subString: string): boolean;
} }
// Only the parameters' types are important, names are not important. // Only the parameters' types are important, names are not important.
var mySearch: SearchFunc; let mySearch: SearchFunc;
mySearch = function(src: string, sub: string) { mySearch = function (src: string, sub: string) {
return src.search(sub) != -1; return src.search(sub) != -1;
} }
@ -104,8 +113,8 @@ class Point {
static origin = new Point(0, 0); static origin = new Point(0, 0);
} }
var p1 = new Point(10 ,20); let p1 = new Point(10, 20);
var p2 = new Point(25); //y will be 0 let p2 = new Point(25); //y will be 0
// Inheritance // Inheritance
class Point3D extends Point { class Point3D extends Point {
@ -115,7 +124,7 @@ class Point3D extends Point {
// Overwrite // Overwrite
dist() { dist() {
var d = super.dist(); let d = super.dist();
return Math.sqrt(d * d + this.z * this.z); 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 // Local alias for referencing a module
import G = Geometry; import G = Geometry;
var s2 = new G.Square(10); let s2 = new G.Square(10);
// Generics // Generics
// Classes // Classes
@ -152,21 +161,21 @@ interface Pair<T> {
} }
// And functions // And functions
var pairToTuple = function<T>(p: Pair<T>) { let pairToTuple = function <T>(p: Pair<T>) {
return new Tuple(p.item1, p.item2); 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: // Including references to a definition file:
/// <reference path="jquery.d.ts" /> /// <reference path="jquery.d.ts" />
// Template Strings (strings that use backticks) // Template Strings (strings that use backticks)
// String Interpolation with Template Strings // String Interpolation with Template Strings
var name = 'Tyrone'; let name = 'Tyrone';
var greeting = `Hi ${name}, how are you?` let greeting = `Hi ${name}, how are you?`
// Multiline Strings with Template Strings // Multiline Strings with Template Strings
var multiline = `This is an example let multiline = `This is an example
of a multiline string`; of a multiline string`;
``` ```