summaryrefslogtreecommitdiffhomepage
path: root/typescript.html.markdown
diff options
context:
space:
mode:
authorDimitri Kokkonis <kokkonisd@gmail.com>2019-08-06 12:40:31 +0200
committerGitHub <noreply@github.com>2019-08-06 12:40:31 +0200
commit922fc494bcce6cb53d80a5c2c9c039a480c82c1f (patch)
treea62b6e194db73630bfed43301b1c130d52f9dbba /typescript.html.markdown
parent5710394756a426255d2dc81d2d342e9786ac2c1b (diff)
parent2f0b904f6ffe68d15fedf7e50a3a64e1a47a9145 (diff)
Merge pull request #1 from adambard/master
Update fork
Diffstat (limited to 'typescript.html.markdown')
-rw-r--r--typescript.html.markdown17
1 files changed, 15 insertions, 2 deletions
diff --git a/typescript.html.markdown b/typescript.html.markdown
index 55d8cefe..cf2111d5 100644
--- a/typescript.html.markdown
+++ b/typescript.html.markdown
@@ -224,6 +224,19 @@ 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
+// Tagged Union Types for modelling state that can be in one of many shapes
+type State =
+ | { type: "loading" }
+ | { type: "success", value: number }
+ | { type: "error", message: string };
+
+declare const state: State;
+if (state.type === "success") {
+ console.log(state.value);
+} else if (state.type === "error") {
+ console.error(state.message);
+}
+
// Iterators and Generators
// for..of statement
@@ -235,13 +248,13 @@ for (const val of arrayOfAnyType) {
let list = [4, 5, 6];
for (const i of list) {
- console.log(i); // "4", "5", "6"
+ console.log(i); // 4, 5, 6
}
// for..in statement
// iterate over the list of keys on the object being iterated
for (const i in list) {
- console.log(i); // "0", "1", "2",
+ console.log(i); // 0, 1, 2
}