Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flow type helpers #2710

Open
fdecampredon opened this issue Apr 10, 2015 · 11 comments
Open

Flow type helpers #2710

fdecampredon opened this issue Apr 10, 2015 · 11 comments
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript

Comments

@fdecampredon
Copy link

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
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Apr 10, 2015
@RyanCavanaugh
Copy link
Member

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

@DanielRosenwasser
Copy link
Member

I discussed with @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.

@Lenne231
Copy link

There is another issue on this #3779

@benliddicott
Copy link

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.

@s-panferov
Copy link

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.

@benliddicott
Copy link

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

@AlexGalays
Copy link

$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.

@basarat
Copy link
Contributor

basarat commented Aug 26, 2016

$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 ¯\_(ツ)_/¯

@dead-claudia
Copy link

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:

@dead-claudia
Copy link

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

@Kinrany
Copy link

Kinrany commented Jan 9, 2019

#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
Labels
Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

10 participants