diff options
author | Divay Prakash <divayprakash@users.noreply.github.com> | 2019-08-03 00:22:02 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-03 00:22:02 +0530 |
commit | 787d57ad9f1959da3ee8e3dd832fe42e5602dd3d (patch) | |
tree | 9b347c72331e854da639d5b1425fad5c31ae89e4 /typescript.html.markdown | |
parent | ed75a2db9e27e9c98599222bc8b95c9b222d4589 (diff) | |
parent | 96b65fb40d84ce97b6ca7b814e210a61bca294df (diff) |
Merge pull request #3558 from hasparus/patch-1
[typescript/en] Add tagged union types
Diffstat (limited to 'typescript.html.markdown')
-rw-r--r-- | typescript.html.markdown | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/typescript.html.markdown b/typescript.html.markdown index 55d8cefe..293a4a35 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 |