Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Last active December 19, 2016 18:35
Show Gist options
  • Save iamdustan/17ced5b2d362d7c68cc147d05a9fb83c to your computer and use it in GitHub Desktop.
Save iamdustan/17ced5b2d362d7c68cc147d05a9fb83c to your computer and use it in GitHub Desktop.
Flow union functions?
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
/** @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