From 0e437a75db091eb5cb057f1a49bf07db562d1d8f Mon Sep 17 00:00:00 2001 From: davidgtu Date: Fri, 11 Oct 2019 15:53:07 -0400 Subject: add type assertion --- typescript.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'typescript.html.markdown') diff --git a/typescript.html.markdown b/typescript.html.markdown index cf2111d5..6c6da2c4 100644 --- a/typescript.html.markdown +++ b/typescript.html.markdown @@ -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' ``` -- 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