-
-
Notifications
You must be signed in to change notification settings - Fork 230
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
$ObjMap support #18
Comments
I would gladly add that to the API, although I'm not sure I'll have time to work on this any time soon. If anyone would want to work on this, PR'a are welcomed. For any contributor here is a use-case example to clarify what this type should achieve: type $ObjMap<T extends Record<string, any>, MapFn extends ((...args: any[]) => any)> = {
[P in keyof T]: MapFn<T[P]>; // MapFn is executed on each value of an object
}
type MapToReturnType<T> = T extends ((...args: any[]) => any) ? ReturnType<T> : never;
const o = {
a: () => true,
b: () => 'foo'
};
function mapValues<T extends {[key: string]: (...args: any[]) => any}>(o: T): $ObjMap<T, MapToReturnType> {
return Object.values(fn => fn());
}
const result = mapValues(o) // result.a is boolean, result.b is 'foo'
result.a // boolean
result.b // 'foo' |
@BoostIO funded this issue with $10. Visit this issue on Issuehunt |
@zetamorph has started working. Visit this issue on Issuehunt |
Hi, I looked around and went through some related TypeScript issues and it seems that this is not possible with TypeScript at the moment. It also seems that this change in the TypeScript compiler will not land for a long time, as can be read here: microsoft/TypeScript#26043 (comment) |
type ObjMap<O extends {}, T> = { [K in keyof O]: T }; works in TS 3.1 (probably in 2.9 too) bcherny/flow-to-typescript#11 Update: this is not the same as original $ObjMap, I thought about different use case |
@malash has submitted a pull request. See it on IssueHunt |
I wanted to refer to some proposals posted here that are different from original $ObjMap in Flow. I know TS doesn't allow this operation right now (please read #18 (comment) above) but we don't want to ship half-working solution as it would be confusing to people try to use it as explained above and flooding us with complains that it doesn't work. This issue will stay open here waiting until TS allow this kind of feature. |
Hi @piotrwitek , type $ObjMap<T extends Record<string, any>> = {
[P in keyof T]: T[P] extends (...args: any[]) => any ? ReturnType<T[P]> : never;
} |
Hey @captain-yossarian, I have added the clarification about this type, please check this comment: #18 (comment) |
@piotrwitek is it possible to run tests on windows? Every time I'm running
|
@piotrwitek I'm not sure it is possible to add second generic argument to $ObjMap as you did it in your example |
It seems that the problem for tests on windows is testMatch path regex, I cannot help with that unfortunately. |
It might not be currently possible, but it's the goal for this type. Otherwise, it'll be a completely different type. |
SO question related to this issue. |
I'm not sure if it's possible (either way might be good to document).
The text was updated successfully, but these errors were encountered: