Skip to content

Commit 136a041

Browse files
authored
Merge pull request #432 from abraham/readme
Document type guards
2 parents be55d41 + 852216c commit 136a041

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ class Thing {
8181
}
8282
```
8383

84+
Using included [type guards](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates).
85+
86+
```ts
87+
import { RemoteData } from '@abraham/remotedata';
88+
import { Status } from 'twitter-d';
89+
import { getState } from './state';
90+
91+
// Define what the RemoteData Failure and Success types will be.
92+
type State = RemoteData<number, Status>;
93+
94+
const state: State = getState();
95+
if (isSuccess(state)) {
96+
// state is known to be of type Success<Status>
97+
console.log(state.data.full_text);
98+
}
99+
```
100+
84101
# References
85102

86103
- [How Elm Slays a UI Antipattern](http://blog.jenkster.com/2016/06/how-elm-slays-a-ui-antipattern.html)

0 commit comments

Comments
 (0)