Last active
May 6, 2020 07:26
-
-
Save drothmaler/2557fc16b55dcae6aec6f4baf98d235d to your computer and use it in GitHub Desktop.
Revisions
-
drothmaler revised this gist
May 6, 2020 . 1 changed file with 24 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ interface Foo { n: number s: string @@ -8,11 +7,13 @@ class Bar1 { n: number s: string foo: Foo } // anything can be changed let bar1 = new Bar1() bar1.n = 42 // <-- Working bar1.s = "foo" // <-- Working bar1.s.length = 0 // <-- Error (string is always immutable) bar1.foo = { // <-- Working n: 21, s: "bar" @@ -24,28 +25,31 @@ class Bar2 { readonly n: number readonly s: string readonly foo: Foo } // properties of Bar2 can't be reassigned let bar2 = new Bar2() bar2.n = 42 // <-- Error bar2.s = "foo" // <-- Error bar2.s.length = 0 // <-- Error bar2.foo = { // <-- Error n: 21, s: "bar" } bar2.foo.n = 42 // <-- Working bar2.foo.s = "foo" // <-- Working class Bar3 { n: Readonly<number> // has no effect s: Readonly<string> // has no effect foo: Readonly<Foo> } // properties of Foo can't be reassigned let bar3 = new Bar3() bar3.n = 42 // <-- Working bar3.s = "foo" // <-- Working bar3.s.length = 0 // <-- Error bar3.foo = { // <-- Working n: 21, s: "bar" @@ -58,4 +62,16 @@ class Bar4 { readonly n: number readonly s: string readonly foo: Readonly<Foo> } // Neither properties of Foo, nor Bar4 can't be reassigned let bar4 = new Bar4() bar4.n = 42 // <-- Error bar4.s = "foo" // <-- Error bar4.s.length = 0 // <-- Error bar4.foo = { // <-- Error n: 21, s: "bar" } bar4.foo.n = 42 // <-- Error bar4.foo.s = "foo" // <-- Error -
drothmaler created this gist
May 6, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,61 @@ interface Foo { n: number s: string } class Bar1 { n: number s: string foo: Foo } let bar1 = new Bar1() bar1.n = 42 // <-- Working bar1.s = "foo" // <-- Working bar1.foo = { // <-- Working n: 21, s: "bar" } bar1.foo.n = 42 // <-- Working bar1.foo.s = "foo" // <-- Working class Bar2 { readonly n: number readonly s: string readonly foo: Foo } let bar2 = new Bar2() bar2.n = 42 // <-- Error bar2.s = "foo" // <-- Error bar2.foo = { // <-- Error n: 21, s: "bar" } bar2.foo.n = 42 // <-- Working bar2.foo.s = "foo" // <-- Working class Bar3 { n: Readonly<number> s: Readonly<string> foo: Readonly<Foo> } let bar3 = new Bar3() bar3.n = 42 // <-- Working bar3.s = "foo" // <-- Working bar3.foo = { // <-- Working n: 21, s: "bar" } bar3.foo.n = 42 // <-- Error bar3.foo.s = "foo" // <-- Error class Bar4 { readonly n: number readonly s: string readonly foo: Readonly<Foo> }