Skip to content

Instantly share code, notes, and snippets.

@nmn
Last active November 3, 2017 12:25
Show Gist options
  • Save nmn/3d7bc1682ea7aad79a416ae901b3abc1 to your computer and use it in GitHub Desktop.
Save nmn/3d7bc1682ea7aad79a416ae901b3abc1 to your computer and use it in GitHub Desktop.

Revisions

  1. nmn renamed this gist Jul 9, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. nmn revised this gist Jul 9, 2016. 1 changed file with 29 additions and 5 deletions.
    34 changes: 29 additions & 5 deletions gistfile1.ts
    Original 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;
    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)

    // 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)
    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)
  3. nmn renamed this gist Jul 9, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. nmn created this gist Jul 9, 2016.
    25 changes: 25 additions & 0 deletions gistfile1.txt
    Original 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)