Repository navigation
Flow type helpers #2710
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptNeeds ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.
on Apr 10, 2015 RyanCavanaugh commented
on Apr 10, 2015 MemberMore actionsSounds neat. It'd probably be best to log these separately if/when more detailed proposals become available.
DanielRosenwasser commented
on Aug 14, 2015 MemberMore actionsI discussed with Seva Ivanov (@wwwsevolod) over Gitter - something like
$ResultOfwould 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
$ResultOfequivalent functionality could be implemented by extendingtypeofto 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
typeofto 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.
Reacted by Lennart, Caleb Meredith, Alexandre Galays, Dean Merchant, moritzvonhase and Anthony AscencioShape 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.assignlike construct.$Enum is the set of keys of T
This became
$Keysand TypeScript is gettingkeysof: #10425FWIW 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¯\_(ツ)_/¯Reacted by Herrington Darkholme and Claudia MeadowsTaking 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 betweenTandS$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:
$Diff<A, B>→ requested in Subtraction types #4183, proposal in Add support for literal type subtraction #12215$Either<...T>→ tied in with the variadic kinds issue in Proposal: Variadic Kinds -- Give specific types to variadic functions #5453$All<...T>→ tied in with the variadic kinds issue in Proposal: Variadic Kinds -- Give specific types to variadic functions #5453
Reacted by Daniel Rosenwasser, Lennart, Bazyli Brzóska, Viacheslav Slinko, Chris Gervang, Alex Boltunov, Karl Horky and Andrey StarovoytBasarat Ali Syed (@basarat) I suspect people started relying on those due to the limitations of Flow's public type-level APIs. 😉
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.