From 3f5251edf9acb6c64d6f3dfadafe587a218b902b Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 14 Aug 2014 12:12:50 +0200 Subject: Intial file created for TypeScript Not all content but it's a good start --- typescript.html.markdown | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 typescript.html.markdown (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown new file mode 100644 index 00000000..f0667d49 --- /dev/null +++ b/typescript.html.markdown @@ -0,0 +1,75 @@ +--- +language: TypeScript +contributors: + - ["Philippe Vlérick", "https://github.com/pvlerick"]] +--- + +TypeScript is a language that aims at easing development of large scale applications written in JavaScript. +TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript. +It is a superset of JavaScript: any JavaScript code is valid TypeScript code so it can be added seamlessly to any project. +TypeScript code compiles down to JavaScript. + +This article will focus only on TypeScript added syntax, everything else is plain [JavaScript] (../javascript/). + +```ts +//There are 4 basic types in TypeScript +var isDone: boolean = false; +var lines: number = 42; +var name: string = "Anders"; + +//However, 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 +var list: number[] = [1, 2, 3]; +//Or, using the generic array type +var list: Array = [1, 2, 3]; + +//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 +function bigHorribleAlert(): void { + alert("I'm a little annoying box!"); +} + +//Interfaces are structural, anything that has the properties is compliant with the interface. +//In the bellow example, any object that has a name which is a string and an age which is a number is a Person. +//This is called "duck typing". +interface Person { + name: string; + age: number; + + //Interfaces also support optional properties + phone?: number; +} + +//Interfaces can also describe a function type, to describe a function signature +interface SearchFunc { + (source: string, subString: string): boolean; +} +//The type can then be used for functions, and the compiler will be able to check that types are compliants +//Note that only the parameters' types are important, names are not important. +var mySearch: SearchFunc; +mySearch = function(src: string, sub: string) { + var result = source.search(subString); + if (result == -1) { + return false; + } + else { + return true; + } +} + + +``` + +To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground). + +## Further Reading + * [TypeScript Official website] (http://www.typescriptlang.org/) + * [TypeScript language specifications (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238) + * [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript) -- cgit v1.2.3 From fda947d93997ac1253c11ebed6a8c615c49c71f5 Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 14 Aug 2014 13:34:41 +0200 Subject: Some re-phrasing and typos --- typescript.html.markdown | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index f0667d49..410cd6e4 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -6,18 +6,19 @@ contributors: TypeScript is a language that aims at easing development of large scale applications written in JavaScript. TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript. -It is a superset of JavaScript: any JavaScript code is valid TypeScript code so it can be added seamlessly to any project. -TypeScript code compiles down to JavaScript. +It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. In turn, the TypeScript compiler transform the code to JavaScript. This article will focus only on TypeScript added syntax, everything else is plain [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 resulting JavaScript. + ```ts -//There are 4 basic types in TypeScript +//There are 3 basic types in TypeScript var isDone: boolean = false; var lines: number = 42; var name: string = "Anders"; -//However, 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 @@ -67,7 +68,7 @@ mySearch = function(src: string, sub: string) { ``` -To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground). + ## Further Reading * [TypeScript Official website] (http://www.typescriptlang.org/) -- cgit v1.2.3 From 7f2256b2e65cb72ef87a5cff0a63bc4982a5e496 Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 14 Aug 2014 15:40:21 +0200 Subject: Tutorial on classes and functions added TODO: more on indexers, check the comment emitted twice for f2 function --- typescript.html.markdown | 82 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 23 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 410cd6e4..3363426a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -6,11 +6,11 @@ contributors: TypeScript is a language that aims at easing development of large scale applications written in JavaScript. 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. In turn, the TypeScript compiler transform the code 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 emitts JavaScript. -This article will focus only on TypeScript added syntax, everything else is plain [JavaScript] (../javascript/). +This article will focus only on TypeScript extra syntax, as oposed 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 resulting 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. ```ts //There are 3 basic types in TypeScript @@ -25,10 +25,10 @@ notSure = false; // okay, definitely a boolean //For collections, there are typed arrays and generic arrays var list: number[] = [1, 2, 3]; -//Or, 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; @@ -37,40 +37,76 @@ function bigHorribleAlert(): void { alert("I'm a little annoying box!"); } -//Interfaces are structural, anything that has the properties is compliant with the interface. -//In the bellow example, any object that has a name which is a string and an age which is a number is a Person. -//This is called "duck typing". +//Functions are first class citizens, have a shortened definition and can leverage the strong type inference +//All examples are equivalent, the same signature will be infered by the compiler and the 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 #TODO bug! +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 + +//Interfaces are structural, anything that has the properties is compliant with the interface (duck typing) interface Person { name: string; - age: number; - - //Interfaces also support optional properties - phone?: number; + //Optional properties, marked with a "?" + age?: number; +} +//Object that implements the "Person" interface +var p : Person = { name: "Bobby" }; //Can be treated as a Person since it has the name and age properties +//Objects that have the optional property: +var validPerson : Person = { name: "Bobby", age: 42 }; +var invalidPerson : Person = { name: "Bobby", age: true }; //Is not a person because age is not a number + +//Interfaces can also define method signatures: +interface PersonWhoCanTalk { + sayHello(otherPersonsName: string): void; +} + +//And also indexers, both with number and string +interface PersonWhoCanBeIndexed { + [index: number]: string; } +//TODO -//Interfaces can also describe a function type, to describe a function signature +//Interfaces can also describe a function type interface SearchFunc { (source: string, subString: string): boolean; } -//The type can then be used for functions, and the compiler will be able to check that types are compliants -//Note that 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) { - var result = source.search(subString); - if (result == -1) { - return false; - } - else { - return true; - } + return src.search(sub) != -1; } +//Classes +class Point { + //Properties + x: number; + + //Constructor - the public keyword is a shortcut to generate the code for a property and it's initialization, equivalent to "x" in this case + constructor(x: number, public y: number) { + 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 p = new Point(10 ,20); +//Generics +//Including references to a definition file +/// + +``` ## Further Reading * [TypeScript Official website] (http://www.typescriptlang.org/) * [TypeScript language specifications (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238) * [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 7d0adf66eab5d391d63c2bcf0fd6b1291d781a22 Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 14 Aug 2014 15:56:30 +0200 Subject: Added inheritance and overwrite examples --- typescript.html.markdown | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 3363426a..3da7bca2 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -83,8 +83,10 @@ class Point { //Properties x: number; - //Constructor - the public keyword is a shortcut to generate the code for a property and it's initialization, equivalent to "x" in this case - constructor(x: number, public y: number) { + //Constructor - the public/private keywords are shortcuts to generate the code for a property and its initialization + //Equivalent to "x" in this case + //Default values are also supported + constructor(x: number, public y: number = 0) { this.x = x; } @@ -95,7 +97,23 @@ class Point { static origin = new Point(0, 0); } -var p = new Point(10 ,20); +var p1 = new Point(10 ,20); +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 + } + + /Overwrite + dist() { + var d = super.dist(); + return Math.sqrt(d * d + this.z * this.z); + } +} + +//Modules //Generics -- cgit v1.2.3 From b5ccc1d83cfbc1d11ca32e44708fc1eb339027c0 Mon Sep 17 00:00:00 2001 From: Philippe Date: Thu, 14 Aug 2014 15:58:34 +0200 Subject: Small typos --- typescript.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 3da7bca2..8209fedb 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -37,8 +37,8 @@ function bigHorribleAlert(): void { alert("I'm a little annoying box!"); } -//Functions are first class citizens, have a shortened definition and can leverage the strong type inference -//All examples are equivalent, the same signature will be infered by the compiler and the same JavaScript will be emitted +//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 var f1 = function(i: number) : number { return i * i; } var f2 = function(i: number) { return i * i; } //Return type infered #TODO bug! var f3 = (i : number) : number => { return i * i; } @@ -106,7 +106,7 @@ class Point3D extends Point { super(x, y); //Explicit call to the super class constructor is mandatory } - /Overwrite + //Overwrite dist() { var d = super.dist(); return Math.sqrt(d * d + this.z * this.z); @@ -117,7 +117,7 @@ class Point3D extends Point { //Generics -//Including references to a definition file +//Including references to a definition file: /// ``` -- cgit v1.2.3 From 0d022b14c0d11314d29211b862ac65095ceba26c Mon Sep 17 00:00:00 2001 From: Philippe Date: Wed, 27 Aug 2014 15:41:12 +0200 Subject: Added section on Modules Indented comments and edited the Interfaces part. Still missing indexers and Generics. --- typescript.html.markdown | 50 ++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 21 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 8209fedb..74e98d14 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -18,7 +18,7 @@ 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 @@ -50,40 +50,32 @@ interface Person { name: string; //Optional properties, marked with a "?" age?: number; -} -//Object that implements the "Person" interface -var p : Person = { name: "Bobby" }; //Can be treated as a Person since it has the name and age properties -//Objects that have the optional property: -var validPerson : Person = { name: "Bobby", age: 42 }; -var invalidPerson : Person = { name: "Bobby", age: true }; //Is not a person because age is not a number - -//Interfaces can also define method signatures: -interface PersonWhoCanTalk { - sayHello(otherPersonsName: string): void; + //And of course functions + move(): void; } -//And also indexers, both with number and string -interface PersonWhoCanBeIndexed { - [index: number]: string; -} -//TODO +//..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: +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 -//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 +//Classes - members are public by default class Point { //Properties x: number; - //Constructor - the public/private keywords are shortcuts to generate the code for a property and its initialization + //Constructor - the public/private keywords in this context are shortcuts to generate the code for a property //Equivalent to "x" in this case //Default values are also supported constructor(x: number, public y: number = 0) { @@ -113,7 +105,23 @@ class Point3D extends Point { } } -//Modules +//Modules, "." can be used as separators for sub modules +module Geometry { + export class Square { + constructor(public sideLength: number = 0) { + } + area() { + return Math.pow(this.sideLength, 2); + } + } +} + +var s1 = new Geometry.Square(5); + +//..Local alias for rreferencing a module +import G = Geometry; + +var s2 = new G.Square(10); //Generics -- cgit v1.2.3 From bfa04112e8e788bea9dc53cdef1659961c7882cb Mon Sep 17 00:00:00 2001 From: Philippe Date: Wed, 27 Aug 2014 16:14:57 +0200 Subject: Added filename in header --- typescript.html.markdown | 1 + 1 file changed, 1 insertion(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 74e98d14..fd22cbef 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -2,6 +2,7 @@ language: TypeScript contributors: - ["Philippe Vlérick", "https://github.com/pvlerick"]] +filename: learntypescript.ts --- TypeScript is a language that aims at easing development of large scale applications written in JavaScript. -- cgit v1.2.3 From 2c56f7bed41f7b7e26989374020d68f1f0f9ebe5 Mon Sep 17 00:00:00 2001 From: Philippe Date: Sun, 31 Aug 2014 17:57:44 +0200 Subject: Generics added And a few typos corrected, ready for pull request --- typescript.html.markdown | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index fd22cbef..3ba1300d 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -41,7 +41,7 @@ function bigHorribleAlert(): void { //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 var f1 = function(i: number) : number { return i * i; } -var f2 = function(i: number) { return i * i; } //Return type infered #TODO bug! +var f2 = function(i: number) { return i * i; } //Return type infered 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 @@ -119,12 +119,30 @@ module Geometry { var s1 = new Geometry.Square(5); -//..Local alias for rreferencing a module +//..Local alias for referencing a module import G = Geometry; var s2 = new G.Square(10); //Generics +//..Classes +class Tuple { + constructor(public item1: T1, public item2: T2) { + } +} + +//..Interfaces +interface Pair { + item1: T; + item2: T; +} + +//..And functions +var pairToTuple = function(p: Pair) { + return new Tuple(p.item1, p.item2); +}; + +var tuple = pairToTuple({ item1:"hello", item2:"world"}); //Including references to a definition file: /// -- cgit v1.2.3 From ef37db634aa8fc1b21a1542c176339f250678cb2 Mon Sep 17 00:00:00 2001 From: Philippe Date: Mon, 1 Sep 2014 10:51:43 +0200 Subject: Fixed errors and typos After review from from Nami-Doc --- typescript.html.markdown | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 3ba1300d..8173aac8 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -1,13 +1,13 @@ --- language: TypeScript contributors: - - ["Philippe Vlérick", "https://github.com/pvlerick"]] + - ["Philippe Vlérick", "https://github.com/pvlerick"] filename: learntypescript.ts --- TypeScript is a language that aims at easing development of large scale applications written in JavaScript. 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 emitts 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/). @@ -46,7 +46,7 @@ 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 -//Interfaces are structural, anything that has the properties is compliant with the interface (duck typing) +//Interfaces are structural, anything that has the properties is compliant with the interface interface Person { name: string; //Optional properties, marked with a "?" @@ -76,8 +76,9 @@ class Point { //Properties x: number; - //Constructor - the public/private keywords in this context are shortcuts to generate the code for a property - //Equivalent to "x" in this case + //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; @@ -106,7 +107,7 @@ class Point3D extends Point { } } -//Modules, "." can be used as separators for sub modules +//Modules, "." can be used as separator for sub modules module Geometry { export class Square { constructor(public sideLength: number = 0) { -- cgit v1.2.3 From eab554a7a7f2869ff7dac9f54acce9a7ed55cfa4 Mon Sep 17 00:00:00 2001 From: Adam Date: Mon, 8 Sep 2014 13:08:28 +0200 Subject: Review docs for added rouge lexers and update those with new highlighters --- 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 8173aac8..9f04169a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -13,7 +13,7 @@ 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. -```ts +```js //There are 3 basic types in TypeScript var isDone: boolean = false; var lines: number = 42; -- cgit v1.2.3 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