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.
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 ofT
$Subtype<T>
is the set of all subtypes ofT
$Shape<T>
matches the shape ofT
$Diff<A, B>
is the difference betweenT
andS
$Enum<T>
is the set of keys ofT
$Record<T>
is the type of objects whose keys are those ofT
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:
@basarat I suspect people started relying on those due to the limitations of Flow's public type-level APIs. ๐