Skip to content

Instantly share code, notes, and snippets.

@wokalski
Last active May 2, 2017 23:26
Show Gist options
  • Save wokalski/f99c38da88b5d376266717d0b1b784ac to your computer and use it in GitHub Desktop.
Save wokalski/f99c38da88b5d376266717d0b1b784ac to your computer and use it in GitHub Desktop.
Result type in javascript with flow
/**
* @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
}
}
@AKST
Copy link

AKST commented May 2, 2017

Don't you lose the prototype of the success value with this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment