Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/* @flow */
import typify from "typify"
class Ok<A> {
value: A
constructor(value: A) {
this.value = value
}
}
class Err {
msg: string
constructor(msg: string) {
this.msg = msg
}
}
typify.record("results", {
results: "array string",
})
type Results = {
results: Array<string>
}
type ResultsResponse =
| Ok<Results>
| Err
function fetchItems() : Promise<Results> {
return fetch("/api/items").then((res) => res.json())
}
function fetchItemsWithValidation() : Promise<ResultsResponse> {
return fetchItems().then((data) => {
if (typify.check("results", data)) {
return new Ok(data)
}
return new Err("Invalid JSON")
})
}
@phamjonathan

This comment has been minimized.

Copy link

@phamjonathan phamjonathan commented Dec 20, 2019

Good example!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.