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