Last active
November 3, 2017 12:25
-
-
Save nmn/3d7bc1682ea7aad79a416ae901b3abc1 to your computer and use it in GitHub Desktop.
Revisions
-
nmn renamed this gist
Jul 9, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nmn revised this gist
Jul 9, 2016 . 1 changed file with 29 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,20 @@ /* @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> @@ -15,11 +24,26 @@ 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) -
nmn renamed this gist
Jul 9, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
nmn created this gist
Jul 9, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ /* @flow */ type $Function1<A, B> = (arg: A) => B; type _Function1Value<A, B, F: $Function1<A, B>> = B; type $Function1Value<F: $Function1<*, *>, A> = _Function1Value<A, *, F>; var toString = (value) => String(value) var toNumber = (value) => parseFloat(value) || 0 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) ;(y: number) // Clearly, this is not the best use-case for this type. function fnValueReturner<V, F: $Function1<V, any>>(fn: F, v: V): $Function1Value<F, V> { return fn(v) } ;(fnValueReturner(toString, 1234): string)