Last active
May 2, 2017 23:26
-
-
Save wokalski/f99c38da88b5d376266717d0b1b784ac to your computer and use it in GitHub Desktop.
Result type in javascript with flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @flow | |
* @providesModule Result | |
*/ | |
'use strict' | |
export const SuccessType = 'SuccessType' | |
export const ErrorType = 'ErrorType' | |
type SuccessT<T> = T & { | |
kind: 'SuccessType' | |
} | |
type ErrorT<T> = { | |
kind: 'ErrorType', | |
error: T | |
} | |
export type Result<S, E> = SuccessT<S> | ErrorT<E> | |
export function Success<T>(object: T): SuccessT<T> { | |
return { | |
kind: SuccessType, | |
...object | |
} | |
} | |
export function Error<T>(error: T): ErrorT<T> { | |
return { | |
kind: ErrorType, | |
error: error | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Don't you lose the prototype of the success value with this?