microsoft/TypeScript

Flow type helpers

fdecampredon opened this issue ยท 11 comments

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

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

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.

There is another issue on this #3779

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.

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.

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

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

$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 ยฏ\_(ใƒ„)_/ยฏ

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:

  • $Diff<A, B> โ†’ requested in #4183, proposal in #12215
  • $Either<...T> โ†’ tied in with the variadic kinds issue in #5453
  • $All<...T> โ†’ tied in with the variadic kinds issue in #5453

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

#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?