Skip to content

Flow type helpers #2710

Description

Flow offer some nice type helpers in his recent versions, while some of them are already addressed by typescript, some could perhaps be helpful.
Here are extracted comments from flow source just for inspiration.

  • $Either<...T> is the union of types ...T
  • $All<...T> is the intersection of types ...T
  • $Supertype acts as any over supertypes of T
  • $Subtype acts as any over subtypes of T
  • $Shape matches the shape of T
  • $Diff<T,S>
  • $Enum is the set of keys of T
  • $Record is the type of objects whose keys are those of T

Activity

  1. RyanCavanaugh commented on Apr 10, 2015

    @RyanCavanaugh
    Member

    Sounds neat. It'd probably be best to log these separately if/when more detailed proposals become available.

  2. DanielRosenwasser commented on Aug 14, 2015

    @DanielRosenwasser
    Member

    I discussed with Seva Ivanov (@wwwsevolod) over Gitter - something like $ResultOf would be decently useful for the resulting type of calling a value of some type. This could return the union of types returned by all call signatures for a type.

  3. Lenne231 commented on Aug 15, 2015

    @Lenne231

    There is another issue on this #3779

  4. benliddicott commented on Sep 7, 2015

    @benliddicott

    It seems like a $ResultOf equivalent functionality could be implemented by extending typeof to permit arbitrary expressions.

    // E.g. func(1) returns {a:1}
    function func<T>(a: T): { a: T } { return { a: a }; };
    
    // Would like to say this:
    // let a: typeof (func(1));
    
    // But we can already say this:
    let a = false ? func(1): void 0; // a is type {a: T}
    
    

    Extending typeof to arbitrary expressions would provide more utility, as you could then also reference the types of instance members and return values:

    class P { 
    instanceProperty: {a: number, b: string};
    }
    
    let c: typeof((<P>(void 0)).instanceProperty.b) = "bval";
    
    
    
    
    

    See also #4640 (comment) asking for something equivalent.

  5. s-panferov commented on Sep 14, 2015

    @s-panferov

    Hi all! Is this stuff is going to be implemented? Our teams is really interested in something like $Shape to improve ReactJS and Redux workflow.

  6. benliddicott commented on Sep 14, 2015

    @benliddicott

    Shape seems to be related to type guards. Is that correct?

  7. AlexGalays commented on Aug 26, 2016

    @AlexGalays

    $Shape apears to be by far the highest in demand from that list. It's basically mandatory to have a typesafe Object.assign like construct.

  8. basarat commented on Aug 26, 2016

    @basarat
    Contributor

    $Enum is the set of keys of T

    This became $Keys and TypeScript is getting keysof : #10425

    FWIW these flow helps aren't officially stable i.e. subject to change. They are using $ like angular used $$ i.e internal stuff that we need but not willing to support in your codebase. Also I might be wrong ¯\_(ツ)_/¯

  9. dead-claudia commented on Dec 13, 2016

    @dead-claudia

    Taking the list (edited for a little more clarity):

    • $Either<...T> is the union of types ...T
    • $All<...T> is the intersection of types ...T
    • $Supertype<T> is the set of all supertypes of T
    • $Subtype<T> is the set of all subtypes of T
    • $Shape<T> matches the shape of T
    • $Diff<A, B> is the difference between T and S
    • $Enum<T> is the set of keys of T
    • $Record<T> is the type of objects whose keys are those of T

    Out of these, here's ones with current equivalents as of 2.1:

    • $Supertype<T> → interface Partial<T> { [K in keyof T]?: T[K]; }
    • $Shape<T> → irrelevant (TypeScript uses structural types)
    • $Subtype<T> → irrelevant (structural types are always subtypes of themselves)
    • $Enum<T> → keyof T
    • $Record<T> → interface Record<T> { [K in keyof T]: T[K]; }

    Out of the remaining:

  10. dead-claudia commented on Dec 13, 2016

    @dead-claudia

    Basarat Ali Syed (@basarat) I suspect people started relying on those due to the limitations of Flow's public type-level APIs. 😉

  11. Kinrany commented on Jan 9, 2019

    @Kinrany

    #12215 has been fixed. Should this issue be closed as a duplicate of #5453?

    #3779 has been closed as a duplicate of this issue. Do variadic types cover Flow's generic meta types as well?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions