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

$ObjMap support #18

Open
rattrayalex opened this issue Apr 3, 2018 · 14 comments · May be fixed by #146
Open

$ObjMap support #18

rattrayalex opened this issue Apr 3, 2018 · 14 comments · May be fixed by #146
Labels

Comments

@rattrayalex
Copy link

I'm not sure if it's possible (either way might be good to document).

@piotrwitek
Copy link
Owner

piotrwitek commented Apr 3, 2018

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'

@IssueHuntBot
Copy link

@BoostIO funded this issue with $10. Visit this issue on Issuehunt

@IssueHuntBot
Copy link

@zetamorph has started working. Visit this issue on Issuehunt

@zetamorph
Copy link

zetamorph commented Oct 3, 2018

Hi,

I looked around and went through some related TypeScript issues and it seems that this is not possible with TypeScript at the moment.
Something like this microsoft/TypeScript#6606 would have to exist in TypeScript to strictly infer the types of all mapped properties of the object.

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)

@stereobooster
Copy link

stereobooster commented Oct 15, 2018

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 malash mentioned this issue Dec 17, 2018
@IssueHuntBot
Copy link

@malash has submitted a pull request. See it on IssueHunt

@piotrwitek
Copy link
Owner

I wanted to refer to some proposals posted here that are different from original $ObjMap in Flow.
The primary use case we would like to solve with this type is to iterate over properties of object and execute a function on each value to get the result. Obviously this has to be done on a type-level only.

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.

@piotrwitek piotrwitek added blocked by limitations blocked by TypeScript limitations and removed accepting PRs labels Dec 20, 2018
@captain-yossarian
Copy link

Hi @piotrwitek ,
Do you mean something like this

type $ObjMap<T extends Record<string, any>> = {
    [P in keyof T]: T[P] extends (...args: any[]) => any ? ReturnType<T[P]> : never;
}

@piotrwitek
Copy link
Owner

Hey @captain-yossarian, I have added the clarification about this type, please check this comment: #18 (comment)

@captain-yossarian
Copy link

captain-yossarian commented Jul 12, 2020

@piotrwitek is it possible to run tests on windows? Every time I'm running npm run test:update, I'm receiving:

Run with `--passWithNoTests` to exit with code 0
In D:\js\utility-types
  35 files checked.
  testMatch: D:/js/utility-types/src\?(*.)+(spec|test).ts?(x) - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 35 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! utility-types@3.9.0 test:update: `jest --config jest.config.json --no-cache -u && dts-jest-remap ./src/*.spec.ts --rename {{basename}}.snap.{{extname}}`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the utility-types@3.9.0 test:update script.

@captain-yossarian captain-yossarian linked a pull request Jul 12, 2020 that will close this issue
8 tasks
@captain-yossarian
Copy link

@piotrwitek I'm not sure it is possible to add second generic argument to $ObjMap as you did it in your example
How to asure TS that MapFn argument has generic parameter?

@piotrwitek
Copy link
Owner

piotrwitek commented Jul 13, 2020

It seems that the problem for tests on windows is testMatch path regex, I cannot help with that unfortunately.

@piotrwitek
Copy link
Owner

piotrwitek commented Jul 13, 2020

It might not be currently possible, but it's the goal for this type. Otherwise, it'll be a completely different type.

@captain-yossarian
Copy link

SO question related to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants