diff options
author | Divay Prakash <divayprakash@users.noreply.github.com> | 2020-01-24 20:02:22 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-24 20:02:22 +0530 |
commit | 9f41d38cec5fb891c5450a39a440b948eaf913e5 (patch) | |
tree | 82636e5e18ad7df9636d7939e2aa1f6abecf58c9 /typescript.html.markdown | |
parent | 7e27297ea555764a5e690f251205d9323b349bb2 (diff) | |
parent | 8e18975548d40ab0de6924ffac38127d7f01fecd (diff) |
Merge branch 'master' into dart-formatting
Diffstat (limited to 'typescript.html.markdown')
-rw-r--r-- | typescript.html.markdown | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/typescript.html.markdown b/typescript.html.markdown index cf2111d5..7e857cc0 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 @@ -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 @@ -257,8 +257,24 @@ for (const i in list) { console.log(i); // 0, 1, 2 } +// Type Assertion +let foo = {} // Creating foo as an empty object +foo.bar = 123 // Error: property 'bar' does not exist on `{}` +foo.baz = 'hello world' // Error: property 'baz' does not exist on `{}` +// Because the inferred type of foo is `{}` (an object with 0 properties), you +// are not allowed to add bar and baz to it. However with type assertion, +// the following will pass: + +interface Foo { + bar: number; + baz: string; +} + +let foo = {} as Foo; // Type assertion here +foo.bar = 123; +foo.baz = 'hello world' ``` |