Last active
December 19, 2016 18:35
-
-
Save iamdustan/17ced5b2d362d7c68cc147d05a9fb83c to your computer and use it in GitHub Desktop.
Flow union functions?
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
result.js:17 | |
17: callback(null, {value: Math.random()}); | |
^^^^ null. This type is incompatible with the expected param type of | |
14: callback : ResultCallback<V, E> | |
^ Error | |
result.js:19 | |
19: callback(new Error('Lolnope')); | |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function call | |
19: callback(new Error('Lolnope')); | |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ undefined (too few arguments, expected default/rest parameters). This type is incompatible with | |
14: callback : ResultCallback<V, E> | |
^ object type | |
result.js:19 | |
19: callback(new Error('Lolnope')); | |
^^^^^^^^^^^^^^^^^^^^ Error. This type is incompatible with the expected param type of | |
3: type Callback<T> = (error: null, value: T) => void; | |
^^^^ null | |
result.js:24 | |
24: const a = doSomething(true, (error, value) => { | |
^ arrow function. Could not decide which case to select | |
14: callback : ResultCallback<V, E> | |
^^^^^^^^^^^^^^^^^^^^ union type | |
Case 1 may work: | |
6: | Callback<R> | |
^^^^^^^^^^^ type application of polymorphic type: type `Callback` | |
But if it doesn't, case 2 looks promising too: | |
7: | Errback<E>; | |
^^^^^^^^^^ type application of polymorphic type: type `Errback` | |
Please provide additional annotation(s) to determine whether case 1 works (or consider merging it with case 2): | |
24: const a = doSomething(true, (error, value) => { | |
^^^^^ parameter `error` | |
24: const a = doSomething(true, (error, value) => { | |
^^^^^ parameter `value` | |
result.js:26 | |
26: return errror; | |
^^^^^^ identifier `errror`. Could not resolve name | |
Found 5 errors | |
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 */ | |
type Callback<T> = (error: null, value: T) => void; | |
type Errback<T> = (error: T, value?: any) => void; | |
type ResultCallback<R, E> = | |
| Callback<R> | |
| Errback<E>; | |
type V = {value: number}; | |
type E = Error; | |
const doSomething = ( | |
thing : boolean, | |
callback : ResultCallback<V, E> | |
) => { | |
if (thing) { | |
callback(null, {value: Math.random()}); | |
} else { | |
callback(new Error('Lolnope')); | |
} | |
}; | |
const a = doSomething(true, (error, value) => { | |
if (error) { | |
return errror; | |
} | |
// ideally, flow would know that value is of type `V` here because error is | |
// null, though currently this is a type error | |
console.log(value); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment