From 0d0b536b28d7f3ed8d35cc557142b15d52299526 Mon Sep 17 00:00:00 2001 From: Simon Siegert Date: Thu, 25 Jan 2018 20:56:33 +0100 Subject: Fix naming of constant --- 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 10f01ebc..acc258b4 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -29,7 +29,7 @@ let notSure: any = 4; notSure = "maybe a string instead"; notSure = false; // okay, definitely a boolean -// Use const keyword for constant variables +// Use const keyword for constants const numLivesForCat = 9; numLivesForCat = 1; // Error -- cgit v1.2.3 From f8ff01a17eff289e6e5e84a129dd29fa9a8bcdff Mon Sep 17 00:00:00 2001 From: Wayne Boka Date: Wed, 3 Oct 2018 16:23:54 -0400 Subject: Add Readonly Examples In references to #2702. Making another commit due to several extra files getting committed. --- typescript.html.markdown | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index acc258b4..db6579e2 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -178,6 +178,37 @@ let greeting = `Hi ${name}, how are you?` let multiline = `This is an example of a multiline string`; +// READONLY: New Feature in TypeScript 3.1 +interface Person { + readonly name: string; + readonly age: number; +} + +var p1: Person = { name: "Tyrone", age: 42 }; +p1.age = 25; // Error, p1.x is read-only + +var p2 = { name: "John", age: 60 }; +var p3: Person = p2; // Ok, read-only alias for p2 +p3.x = 35; // Error, p3.x is read-only +p2.x = 45; // Ok, but also changes p3.x because of aliasing + +class Car { + readonly make: string; + readonly model: string; + readonly year = 2018; + + constructor() { + this.make = "Unknown Make"; // Assignment permitted in constructor + this.model = "Unknown Model"; // Assignment permitted in constructor + } +} + +let numbers: Array = [0, 1, 2, 3, 4]; +let moreNumbers: ReadonlyArray = numbers; +moreNumbers[5] = 5; // Error, elements are read-only +moreNumbers.push(5); // Error, no push method (because it mutates array) +moreNumbers.length = 3; // Error, length is read-only +numbers = moreNumbers; // Error, mutating methods are missing ``` ## Further Reading -- cgit v1.2.3 From 67730b4e0b3100a7a6df925ef28606b71aa30a43 Mon Sep 17 00:00:00 2001 From: Bradley Kemp Date: Fri, 19 Oct 2018 13:55:51 +0100 Subject: Add example of "implements" keyword --- typescript.html.markdown | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index db6579e2..9158f123 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -113,6 +113,13 @@ class Point { static origin = new Point(0, 0); } +// Classes can be explicitly marked as implementing an interface. +// Any missing properties will then cause an error at compile-time. +class PointPerson implements Person { + name: string + move() {} +} + let p1 = new Point(10, 20); let p2 = new Point(25); //y will be 0 -- cgit v1.2.3 From a41c5d77c29c6775e060a1f559584810e1390bd8 Mon Sep 17 00:00:00 2001 From: Kyle Mendes Date: Tue, 30 Oct 2018 12:40:01 -0500 Subject: Updating the line length to 80 --- typescript.html.markdown | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 9158f123..6feaca45 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -5,13 +5,19 @@ contributors: 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 emits JavaScript. +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 emits JavaScript. -This article will focus only on TypeScript extra syntax, as opposed to [JavaScript](/docs/javascript). +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. +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 @@ -19,7 +25,8 @@ 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 +// But you can omit the type annotation if the variables are derived +// from explicit literals let isDone = false; let lines = 42; let name = "Anders"; @@ -114,7 +121,7 @@ class Point { } // Classes can be explicitly marked as implementing an interface. -// Any missing properties will then cause an error at compile-time. +// Any missing properties will then cause an error at compile-time. class PointPerson implements Person { name: string move() {} -- cgit v1.2.3 From 217e32442cfd9550ad53f92ce1470527ed1b89a9 Mon Sep 17 00:00:00 2001 From: Elton Maiyo Date: Sun, 27 Jan 2019 18:06:34 +0300 Subject: Fixes typo --- typescript.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 6feaca45..ba4a9e71 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -203,8 +203,8 @@ p1.age = 25; // Error, p1.x is read-only var p2 = { name: "John", age: 60 }; var p3: Person = p2; // Ok, read-only alias for p2 -p3.x = 35; // Error, p3.x is read-only -p2.x = 45; // Ok, but also changes p3.x because of aliasing +p3.age = 35; // Error, p3.age is read-only +p2.age = 45; // Ok, but also changes p3.age because of aliasing class Car { readonly make: string; -- cgit v1.2.3 From 145e76d3b318eab8ff78980c57f34e622a1988b2 Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Mon, 17 Jun 2019 14:52:58 +0530 Subject: Iterators and Generators --- typescript.html.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index ba4a9e71..9f0f4c6d 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -223,6 +223,25 @@ moreNumbers[5] = 5; // Error, elements are read-only moreNumbers.push(5); // Error, no push method (because it mutates array) moreNumbers.length = 3; // Error, length is read-only numbers = moreNumbers; // Error, mutating methods are missing + +// Iterators and Generators + +// for..of statement +// iterate over the list of values on the object being iterated +let list = [4, 5, 6]; +for (let i of list) { + console.log(i); // "4", "5", "6" +} + +// for..in statement +// iterate over the list of keys on the object being iterated +for (let i in list) { + console.log(i); // "0", "1", "2", +} + + + + ``` ## Further Reading -- cgit v1.2.3 From 1b67ecb30c8c634d7f1688696979798e658ca936 Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Mon, 17 Jun 2019 14:56:28 +0530 Subject: Add one more example --- typescript.html.markdown | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 9f0f4c6d..827f202a 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -228,6 +228,11 @@ numbers = moreNumbers; // Error, mutating methods are missing // for..of statement // iterate over the list of values on the object being iterated +let arrayOfAnyType = [1, "string", false]; +for (let val of arrayOfAnyType) { + console.log(val); // 1, "string", false +} + let list = [4, 5, 6]; for (let i of list) { console.log(i); // "4", "5", "6" -- cgit v1.2.3 From ff3dd723599190ccb1b5454f297d116b5c446c70 Mon Sep 17 00:00:00 2001 From: ashraf-patel Date: Tue, 18 Jun 2019 12:21:00 +0530 Subject: PR changes: use const instead of let --- typescript.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 827f202a..55d8cefe 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -229,18 +229,18 @@ numbers = moreNumbers; // Error, mutating methods are missing // for..of statement // iterate over the list of values on the object being iterated let arrayOfAnyType = [1, "string", false]; -for (let val of arrayOfAnyType) { +for (const val of arrayOfAnyType) { console.log(val); // 1, "string", false } let list = [4, 5, 6]; -for (let i of list) { +for (const i of list) { console.log(i); // "4", "5", "6" } // for..in statement // iterate over the list of keys on the object being iterated -for (let i in list) { +for (const i in list) { console.log(i); // "0", "1", "2", } -- cgit v1.2.3 From 96b65fb40d84ce97b6ca7b814e210a61bca294df Mon Sep 17 00:00:00 2001 From: Piotr Monwid-Olechnowicz Date: Wed, 3 Jul 2019 23:15:10 +0200 Subject: [typescript/en] Add tagged union types They're super useful. https://www.typescriptlang.org/docs/handbook/advanced-types.html#discriminated-unions https://mariusschulz.com/blog/typescript-2-0-tagged-union-types --- typescript.html.markdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 55d8cefe..293a4a35 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -224,6 +224,19 @@ moreNumbers.push(5); // Error, no push method (because it mutates array) moreNumbers.length = 3; // Error, length is read-only numbers = moreNumbers; // Error, mutating methods are missing +// Tagged Union Types for modelling state that can be in one of many shapes +type State = + | { type: "loading" } + | { type: "success", value: number } + | { type: "error", message: string }; + +declare const state: State; +if (state.type === "success") { + console.log(state.value); +} else if (state.type === "error") { + console.error(state.message); +} + // Iterators and Generators // for..of statement -- cgit v1.2.3 From 7fa8cc63c85f331e81a51443d9a81a9e058e8f42 Mon Sep 17 00:00:00 2001 From: Monis Qadri Date: Wed, 31 Jul 2019 13:06:54 +0530 Subject: Update typescript.html.markdown The input list is of int[] [@line236](https://github.com/adambard/learnxinyminutes-docs/blame/master/typescript.html.markdown#L236]) but the output of its shown is in string [@line238](https://github.com/adambard/learnxinyminutes-docs/blame/master/typescript.html.markdown#L238]). same thing happens at [@line244](https://github.com/adambard/learnxinyminutes-docs/blame/master/typescript.html.markdown#L244]) --- typescript.html.markdown | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index 55d8cefe..c27e93a6 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -235,13 +235,13 @@ for (const val of arrayOfAnyType) { let list = [4, 5, 6]; for (const i of list) { - console.log(i); // "4", "5", "6" + console.log(i); // 4, 5, 6 } // for..in statement // iterate over the list of keys on the object being iterated for (const i in list) { - console.log(i); // "0", "1", "2", + console.log(i); // 0, 1, 2 } -- cgit v1.2.3 From fb48be47d6cac609cf9d29caae5d4e73df3e214a Mon Sep 17 00:00:00 2001 From: Ross Mackay Date: Tue, 22 Oct 2019 13:16:52 +0100 Subject: Fix outdated comment in en/th-th typescript 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 cf2111d5..6f238d5b 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -199,7 +199,7 @@ interface Person { } var p1: Person = { name: "Tyrone", age: 42 }; -p1.age = 25; // Error, p1.x is read-only +p1.age = 25; // Error, p1.age is read-only var p2 = { name: "John", age: 60 }; var p3: Person = p2; // Ok, read-only alias for p2 -- cgit v1.2.3 From 5a7d3e898b11fb09cfa448e924ef09970b071a51 Mon Sep 17 00:00:00 2001 From: Kyle Mendes Date: Thu, 31 Oct 2019 18:42:00 -0500 Subject: Fix playground link --- 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 6f238d5b..a4f1423f 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -16,7 +16,7 @@ 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 +[Playground](http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript. ```ts -- cgit v1.2.3 From d430cd82b6c00d0df4aa637697caaaf490cda191 Mon Sep 17 00:00:00 2001 From: Kyle Mendes Date: Mon, 4 Nov 2019 14:11:16 -0600 Subject: Updating the TS playground link --- 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 a4f1423f..00f0cbc5 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -16,7 +16,7 @@ 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 +[Playground](https://www.typescriptlang.org/play) where you will be able to type code, have auto completion and directly see the emitted JavaScript. ```ts -- cgit v1.2.3