Last active
November 3, 2017 12:25
-
-
Save nmn/3d7bc1682ea7aad79a416ae901b3abc1 to your computer and use it in GitHub Desktop.
Magic Flow Type 1: $FunctionXValue —— A magic flow type that takes a function type that takes one arg, the type of that arg, and returns the return value type of the function.
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 $Function1<A, B> = (arg: A) => B; | |
type _Function1Value<A, B, F: $Function1<A, B>> = B; // eslint-disable-line | |
type $Function1Value<F: $Function1<*, *>, A> = _Function1Value<A, *, F>; | |
var toString = (value) => String(value) | |
var toNumber = (value) => parseFloat(value) || 0 | |
type NumberXString = ((value: number) => string) & ((value: string) => number) | |
// var numberXString: NumberXString = (value: number | string) => { | |
// if (typeof value === 'string') { | |
// return 1 | |
// } | |
// return String(value) | |
// } | |
type ToStringRetVal = $Function1Value<typeof toString, any> | |
type ToNumberRetVal = $Function1Value<typeof toNumber, any> | |
;(toString: (val: any) => ToStringRetVal) | |
declare var x: ToStringRetVal | |
declare var y: ToNumberRetVal | |
;(x: string) | |
// $ExpectError | |
;(x: boolean) | |
// $ExpectError | |
;(x: number) | |
;(y: number) | |
// $ExpectError | |
;(y: string) | |
function fnValueReturner<V, F: $Function1<V, any>> (fn: F, v: V): $Function1Value<F, V> { | |
return fn(v) | |
} | |
;(fnValueReturner(toString, 1234): string) | |
// $ExpectError | |
;(fnValueReturner(toString, 1234): number) | |
type ShouldBeNumber = $Function1Value<NumberXString, string> | |
declare var num: ShouldBeNumber | |
;(num: number) | |
// $ExpectError | |
;(num: string) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Coul you create the same for extracting the argument type?
There is my try, but id doesn't work