From 2ae3d41c2749645fec4748c764ccd5c9405424e5 Mon Sep 17 00:00:00 2001 From: Nolan Prescott Date: Tue, 10 Mar 2015 15:09:35 -0500 Subject: Fix formatting, close #990 Cleaned up mixed tabs/spaces Wrapped lines at 80 characters Fixed incorrect comment regarding property name --- typescript.html.markdown | 125 ++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 57 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 9f04169a..937ebda1 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -14,100 +14,111 @@ This article will focus only on TypeScript extra syntax, as oposed to [JavaScrip To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. ```js -//There are 3 basic types in TypeScript +// There are 3 basic types in TypeScript var isDone: boolean = false; var lines: number = 42; var name: string = "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; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean -//For collections, there are typed arrays and generic arrays +// For collections, there are typed arrays and generic arrays var list: number[] = [1, 2, 3]; -//Alternatively, using the generic array type +// Alternatively, using the generic array type var list: Array = [1, 2, 3]; -//For enumerations: +// For enumerations: enum Color {Red, Green, Blue}; var c: Color = Color.Green; -//Lastly, "void" is used in the special case of a function not returning anything +// Lastly, "void" is used in the special case of a function returning nothing function bigHorribleAlert(): void { alert("I'm a little annoying box!"); } -//Functions are first class citizens, support the lambda "fat arrow" syntax and use type inference -//All examples are equivalent, the same signature will be infered by the compiler, and same JavaScript will be emitted +// Functions are first class citizens, support the lambda "fat arrow" syntax and +// use type inference + +// 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; } -var f2 = function(i: number) { return i * i; } //Return type infered +// Return type inferred +var f2 = function(i: number) { return i * i; } var f3 = (i : number) : number => { return i * i; } -var f4 = (i: number) => { return i * i; } //Return type infered -var f5 = (i: number) => i * i; //Return type infered, one-liner means no return keyword needed +// Return type inferred +var f4 = (i: number) => { return i * i; } +// Return type inferred, one-liner means no return keyword needed +var f5 = (i: number) => i * i; -//Interfaces are structural, anything that has the properties is compliant with the interface +// Interfaces are structural, anything that has the properties is compliant with +// the interface interface Person { name: string; - //Optional properties, marked with a "?" + // Optional properties, marked with a "?" age?: number; - //And of course functions + // And of course functions move(): void; } -//..Object that implements the "Person" interface -var p : Person = { name: "Bobby", move : () => {} }; //Can be treated as a Person since it has the name and age properties -//..Objects that have the optional property: +// 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 : () => {} }; +// Objects that have the optional property: var validPerson : Person = { name: "Bobby", age: 42, move: () => {} }; -var invalidPerson : Person = { name: "Bobby", age: true }; //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 }; -//..Interfaces can also describe a function type +// Interfaces can also describe a function type interface SearchFunc { (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; mySearch = function(src: string, sub: string) { return src.search(sub) != -1; } -//Classes - members are public by default +// Classes - members are public by default class Point { - //Properties - x: number; - - //Constructor - the public/private keywords in this context will generate the boiler plate code - // for the property and the initialization in the constructor. - // In this example, "y" will be defined just like "x" is, but with less code - //Default values are also supported - constructor(x: number, public y: number = 0) { - this.x = x; - } - - //Functions - dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - - //Static members - static origin = new Point(0, 0); + // Properties + x: number; + + // Constructor - the public/private keywords in this context will generate + // the boiler plate code for the property and the initialization in the + // constructor. + // In this example, "y" will be defined just like "x" is, but with less code + // Default values are also supported + + constructor(x: number, public y: number = 0) { + this.x = x; + } + + // Functions + dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + + // Static members + static origin = new Point(0, 0); } var p1 = new Point(10 ,20); var p2 = new Point(25); //y will be 0 -//Inheritance +// Inheritance class Point3D extends Point { - constructor(x: number, y: number, public z: number = 0) { - super(x, y); //Explicit call to the super class constructor is mandatory - } - - //Overwrite - dist() { - var d = super.dist(); - return Math.sqrt(d * d + this.z * this.z); - } + constructor(x: number, y: number, public z: number = 0) { + super(x, y); // Explicit call to the super class constructor is mandatory + } + + // Overwrite + dist() { + var d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } } -//Modules, "." can be used as separator for sub modules +// Modules, "." can be used as separator for sub modules module Geometry { export class Square { constructor(public sideLength: number = 0) { @@ -120,32 +131,32 @@ module Geometry { var s1 = new Geometry.Square(5); -//..Local alias for referencing a module +// Local alias for referencing a module import G = Geometry; var s2 = new G.Square(10); -//Generics -//..Classes +// Generics +// Classes class Tuple { constructor(public item1: T1, public item2: T2) { } } -//..Interfaces +// Interfaces interface Pair { - item1: T; - item2: T; + item1: T; + item2: T; } -//..And functions +// And functions var pairToTuple = function(p: Pair) { - return new Tuple(p.item1, p.item2); + return new Tuple(p.item1, p.item2); }; var tuple = pairToTuple({ item1:"hello", item2:"world"}); -//Including references to a definition file: +// Including references to a definition file: /// ``` -- cgit v1.2.3 From 394e7ecb84c68983f0bb210a8286cca0ff29e6d1 Mon Sep 17 00:00:00 2001 From: Nolan Prescott Date: Tue, 10 Mar 2015 15:35:00 -0500 Subject: whitespace typo, fix #989 --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 937ebda1..662af494 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -46,7 +46,7 @@ function bigHorribleAlert(): void { var f1 = function(i: number) : number { return i * i; } // Return type inferred var f2 = function(i: number) { return i * i; } -var f3 = (i : number) : number => { return i * i; } +var f3 = (i: number) : number => { return i * i; } // Return type inferred var f4 = (i: number) => { return i * i; } // Return type inferred, one-liner means no return keyword needed -- cgit v1.2.3 From 69480d51b82fdc7d2ad3d035c5744d85f56af807 Mon Sep 17 00:00:00 2001 From: Nolan Prescott Date: Tue, 10 Mar 2015 16:58:18 -0500 Subject: fix spacing issue --- typescript.html.markdown | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 662af494..27a1f71a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -43,14 +43,14 @@ 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; } +var f1 = function(i: number): number { return i * i; } // Return type inferred var f2 = function(i: number) { return i * i; } -var f3 = (i: number) : number => { return i * i; } +var f3 = (i: number): number => { return i * i; } // Return type inferred var f4 = (i: number) => { return i * i; } // Return type inferred, one-liner means no return keyword needed -var f5 = (i: number) => i * i; +var f5 = (i: number) => i * i; // Interfaces are structural, anything that has the properties is compliant with // the interface @@ -64,11 +64,11 @@ 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 : () => {} }; +var p: Person = { name: "Bobby", move: () => {} }; // Objects that have the optional property: -var validPerson : Person = { name: "Bobby", age: 42, move: () => {} }; +var validPerson: Person = { name: "Bobby", age: 42, move: () => {} }; // Is not a person because age is not a number -var invalidPerson : Person = { name: "Bobby", age: true }; +var invalidPerson: Person = { name: "Bobby", age: true }; // Interfaces can also describe a function type interface SearchFunc { @@ -84,7 +84,7 @@ mySearch = function(src: string, sub: string) { class Point { // Properties x: number; - + // Constructor - the public/private keywords in this context will generate // the boiler plate code for the property and the initialization in the // constructor. @@ -94,10 +94,10 @@ class Point { constructor(x: number, public y: number = 0) { this.x = x; } - + // Functions dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - + // Static members static origin = new Point(0, 0); } @@ -110,7 +110,7 @@ class Point3D extends Point { constructor(x: number, y: number, public z: number = 0) { super(x, y); // Explicit call to the super class constructor is mandatory } - + // Overwrite dist() { var d = super.dist(); -- cgit v1.2.3 From 107861875f63baa50caf3ea79c5a73f7b77734c1 Mon Sep 17 00:00:00 2001 From: Eugene Sharygin Date: Tue, 21 Apr 2015 23:01:08 +0300 Subject: [typescript/en] Fix typo --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 27a1f71a..e9135510 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -9,7 +9,7 @@ TypeScript is a language that aims at easing development of large scale applicat TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript. It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript. -This article will focus only on TypeScript extra syntax, as oposed to [JavaScript] (../javascript/). +This article will focus only on TypeScript extra syntax, as opposed to [JavaScript] (../javascript/). To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. -- cgit v1.2.3 From c3387b8a611b6fbb00ac8d54102b772d44d3eca2 Mon Sep 17 00:00:00 2001 From: Andrew Backes Date: Sun, 18 Oct 2015 22:32:14 -0500 Subject: [typescript/en] Template Strings --- typescript.html.markdown | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index e9135510..101bb5dc 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -159,6 +159,10 @@ var tuple = pairToTuple({ item1:"hello", item2:"world"}); // Including references to a definition file: /// +// Template Strings +var name = 'Tyrone'; +var greeting = `Hi ${name}, how are you?` + ``` ## Further Reading -- cgit v1.2.3 From 525b6106a57baa570734e29b43f861cd8cc5de3a Mon Sep 17 00:00:00 2001 From: Andrew Backes Date: Sun, 18 Oct 2015 22:38:56 -0500 Subject: [typescript/en] Multiline Strings Multiline Strings via Template Strings --- typescript.html.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 101bb5dc..61b0fdb8 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -159,9 +159,13 @@ var tuple = pairToTuple({ item1:"hello", item2:"world"}); // Including references to a definition file: /// -// Template Strings +// Template Strings (strings that use backtics) +// String Intepolation with Template Strings var name = 'Tyrone'; var greeting = `Hi ${name}, how are you?` +// Multiline Strings with Template Strings +var multiline = `This is an example +of a multiline string`; ``` -- cgit v1.2.3 From 998e76c310422e0946431b036eb7de03a9384e74 Mon Sep 17 00:00:00 2001 From: Andrew Backes Date: Mon, 19 Oct 2015 08:09:58 -0500 Subject: [typescript/en] Typo fixed in 'backticks' --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 61b0fdb8..17c56e7a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -159,7 +159,7 @@ var tuple = pairToTuple({ item1:"hello", item2:"world"}); // Including references to a definition file: /// -// Template Strings (strings that use backtics) +// Template Strings (strings that use backticks) // String Intepolation with Template Strings var name = 'Tyrone'; var greeting = `Hi ${name}, how are you?` -- cgit v1.2.3 From 5ca38bf934801c9e85e25e19842a6f84ebe6efa0 Mon Sep 17 00:00:00 2001 From: Andrew Backes Date: Mon, 19 Oct 2015 10:40:57 -0500 Subject: [typescript/en] Fixing typo in 'Interpolation' --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 17c56e7a..20974304 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -160,7 +160,7 @@ var tuple = pairToTuple({ item1:"hello", item2:"world"}); /// // Template Strings (strings that use backticks) -// String Intepolation with Template Strings +// String Interpolation with Template Strings var name = 'Tyrone'; var greeting = `Hi ${name}, how are you?` // Multiline Strings with Template Strings -- cgit v1.2.3 From 3c048d79adddea0e8ada7ff2af6445160ac000fc Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 27 Nov 2015 15:30:23 -0800 Subject: Make typescript indentation consistently 2 spaces --- typescript.html.markdown | 52 ++++++++++++++++++++++++------------------------ 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index e9135510..26f1fcd9 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -83,23 +83,23 @@ mySearch = function(src: string, sub: string) { // Classes - members are public by default class Point { // Properties - x: number; + x: number; - // Constructor - the public/private keywords in this context will generate - // the boiler plate code for the property and the initialization in the - // constructor. - // In this example, "y" will be defined just like "x" is, but with less code - // Default values are also supported + // Constructor - the public/private keywords in this context will generate + // the boiler plate code for the property and the initialization in the + // constructor. + // In this example, "y" will be defined just like "x" is, but with less code + // Default values are also supported - constructor(x: number, public y: number = 0) { - this.x = x; - } + constructor(x: number, public y: number = 0) { + this.x = x; + } - // Functions - dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } + // Functions + dist() { return Math.sqrt(this.x * this.x + this.y * this.y); } - // Static members - static origin = new Point(0, 0); + // Static members + static origin = new Point(0, 0); } var p1 = new Point(10 ,20); @@ -107,15 +107,15 @@ var p2 = new Point(25); //y will be 0 // Inheritance class Point3D extends Point { - constructor(x: number, y: number, public z: number = 0) { - super(x, y); // Explicit call to the super class constructor is mandatory - } + constructor(x: number, y: number, public z: number = 0) { + super(x, y); // Explicit call to the super class constructor is mandatory + } - // Overwrite - dist() { - var d = super.dist(); - return Math.sqrt(d * d + this.z * this.z); - } + // Overwrite + dist() { + var d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } } // Modules, "." can be used as separator for sub modules @@ -139,19 +139,19 @@ var s2 = new G.Square(10); // Generics // Classes class Tuple { - constructor(public item1: T1, public item2: T2) { - } + constructor(public item1: T1, public item2: T2) { + } } // Interfaces interface Pair { - item1: T; - item2: T; + item1: T; + item2: T; } // And functions var pairToTuple = function(p: Pair) { - return new Tuple(p.item1, p.item2); + return new Tuple(p.item1, p.item2); }; var tuple = pairToTuple({ item1:"hello", item2:"world"}); -- cgit v1.2.3 From 6136efa772228ce6ac6d6c98f33efd1b864fab0e Mon Sep 17 00:00:00 2001 From: Sergey Isaev Date: Tue, 26 Apr 2016 03:58:30 +0400 Subject: Fix url for language specifications --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 47e41405..260e1d55 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -171,7 +171,7 @@ of a multiline string`; ## Further Reading * [TypeScript Official website] (http://www.typescriptlang.org/) - * [TypeScript language specifications (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238) + * [TypeScript language specifications (pdf)] (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md) * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript) * [Source Code on GitHub] (https://github.com/Microsoft/TypeScript) * [Definitely Typed - repository for type definitions] (http://definitelytyped.org/) -- cgit v1.2.3 From 343b7e20cad729e35f86a0563d943f68903f9752 Mon Sep 17 00:00:00 2001 From: Sergey Isaev Date: Tue, 26 Apr 2016 03:59:06 +0400 Subject: Update typescript.html.markdown --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 260e1d55..21f1ce7d 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -171,7 +171,7 @@ of a multiline string`; ## Further Reading * [TypeScript Official website] (http://www.typescriptlang.org/) - * [TypeScript language specifications (pdf)] (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md) + * [TypeScript language specifications] (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md) * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript) * [Source Code on GitHub] (https://github.com/Microsoft/TypeScript) * [Definitely Typed - repository for type definitions] (http://definitelytyped.org/) -- cgit v1.2.3 From d9caa2436e6ebe63e9b2287725c4e104ddcb88cb Mon Sep 17 00:00:00 2001 From: Gabriel Gomes Date: Sun, 26 Jun 2016 10:03:09 -0300 Subject: Added typescript-pt.html.markdown (#1699) * link for learning of javascript adjusted * Added typescript-pt.html.markdown --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 21f1ce7d..1d712369 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -9,7 +9,7 @@ TypeScript is a language that aims at easing development of large scale applicat TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript. It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript. -This article will focus only on TypeScript extra syntax, as opposed to [JavaScript] (../javascript/). +This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](javascript.html.markdown). To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. -- cgit v1.2.3 From 03bab0e47b38c113e26c064b11cbd171675214a1 Mon Sep 17 00:00:00 2001 From: Thien Do Date: Mon, 21 Nov 2016 02:31:22 +0700 Subject: Use ts for typescript syntax --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 1d712369..204b501a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -13,7 +13,7 @@ This article will focus only on TypeScript extra syntax, as opposed to [JavaScri To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. -```js +```ts // There are 3 basic types in TypeScript var isDone: boolean = false; var lines: number = 42; -- cgit v1.2.3 From ac99d3f1cba2d49a7b2560fe338f6a14044a6eaa Mon Sep 17 00:00:00 2001 From: Thien Do Date: Mon, 21 Nov 2016 03:41:16 +0700 Subject: [typescript/en-us] Clarify function section in typescript (#2575) * Clarify function section in typescript * one-liner to braceless --- typescript.html.markdown | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 1d712369..23e0bab4 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -46,10 +46,12 @@ function bigHorribleAlert(): void { var f1 = function(i: number): number { return i * i; } // Return type inferred var f2 = function(i: number) { return i * i; } +// "Fat arrow" syntax var f3 = (i: number): number => { return i * i; } -// Return type inferred +// "Fat arrow" syntax with return type inferred var f4 = (i: number) => { return i * i; } -// Return type inferred, one-liner means no return keyword needed +// "Fat arrow" syntax with return type inferred, braceless means no return +// keyword needed var f5 = (i: number) => i * i; // Interfaces are structural, anything that has the properties is compliant with -- cgit v1.2.3 From 1ac2f69479caef3218e3bf8ab6d88351af9ed3eb Mon Sep 17 00:00:00 2001 From: bezigon Date: Thu, 25 May 2017 18:35:08 +0300 Subject: Update typescript.html.markdown (#2738) https://github.com/adambard/learnxinyminutes-docs/issues/2737 --- typescript.html.markdown | 65 +++++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 28 deletions(-) (limited to 'typescript.html.markdown') 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 = [1, 2, 3]; +let list: Array = [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 { } // And functions -var pairToTuple = function(p: Pair) { +let pairToTuple = function (p: Pair) { 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: /// // 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`; ``` -- cgit v1.2.3 From 985d23a52b76593a120adff5381c2df3a80fe298 Mon Sep 17 00:00:00 2001 From: HairyFotr Date: Wed, 23 Aug 2017 10:14:39 +0200 Subject: Fix a bunch of typos --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index ef37182d..44fd791a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -50,7 +50,7 @@ function bigHorribleAlert(): void { // Functions are first class citizens, support the lambda "fat arrow" syntax and // use type inference -// The following are equivalent, the same signature will be infered by the +// The following are equivalent, the same signature will be inferred by the // compiler, and same JavaScript will be emitted let f1 = function (i: number): number { return i * i; } // Return type inferred -- cgit v1.2.3 From 838efd36532faf52d631bbc798b284bbc7867479 Mon Sep 17 00:00:00 2001 From: Allwin Baby Panikulam Date: Thu, 26 Oct 2017 01:12:10 +0800 Subject: Fix link to javascript docs --- typescript.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 44fd791a..10f01ebc 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -9,7 +9,7 @@ TypeScript is a language that aims at easing development of large scale applicat TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript. It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript. -This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](javascript.html.markdown). +This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](/docs/javascript). To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. -- cgit v1.2.3