Skip to content

Instantly share code, notes, and snippets.

@deepak
Last active August 9, 2017 12:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deepak/2c01ea70b2a6f8338ac548aac4ac204f to your computer and use it in GitHub Desktop.
Save deepak/2c01ea70b2a6f8338ac548aac4ac204f to your computer and use it in GitHub Desktop.
flow has no Maybe type
// @flow
// https://flow.org/en/docs/types/unions/#toc-disjoint-unions
type Response = {
success: boolean,
value?: boolean,
error?: string
};
// works.
// but no Maybe type like [Elm](http://package.elm-lang.org/packages/elm-lang/core/latest/Maybe)
// flow will enforce check for nulls. but Elm is much nicer as there is a type :-)
function handleResponse1(response: Response) {
if (response.success) {
if (response.value) {
var value: boolean = response.value;
}
} else {
if (response.error) {
var error: string = response.error;
}
}
}
// fails.
function handleResponse2(response: Response) {
if (response.success) {
var value1: boolean = response.value;
var value2: boolean | void = response.value; // works
} else {
var error1: string = response.error;
var error2: string | void = response.error; // works
}
}
/////////////////////////////////////////////////
// Error: src/main.js:28
// 28: var value1: boolean = response.value;
// ^^^^^^^^^^^^^^ undefined. This type is incompatible with
// 28: var value1: boolean = response.value;
// ^^^^^^^ boolean
//
// Error: src/main.js:31
// 31: var error1: string = response.error;
// ^^^^^^^^^^^^^^ undefined. This type is incompatible with
// 31: var error1: string = response.error;
// ^^^^^^ string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment