diff options
author | Divay Prakash <divayprakash@users.noreply.github.com> | 2018-10-24 11:04:05 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-24 11:04:05 +0530 |
commit | 4a7d678c2553bc379542a5901177b8bb2730ce65 (patch) | |
tree | b2896f9f02b5a752f860a71a36b1e16c566a8ed7 /typescript.html.markdown | |
parent | e2949649f054ca069e95a05b04d99bccc30ba45d (diff) | |
parent | 8f5a67190705c9a3101653901d8f8a7b48eb1775 (diff) |
Merge branch 'master' into master
Diffstat (limited to 'typescript.html.markdown')
-rw-r--r-- | typescript.html.markdown | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/typescript.html.markdown b/typescript.html.markdown index 10f01ebc..9158f123 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 @@ -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 @@ -178,6 +185,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<number> = [0, 1, 2, 3, 4]; +let moreNumbers: ReadonlyArray<number> = 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 |