-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
When used with objects, the return type unifies all value types #16
Comments
If anyone wants to work on this, see the initial attempt and feedback in #17. |
We have a lot of type-casts in our codebase as well because of this issue and it looks like #17 would have worked well for us and the problem described here. Could you maybe explain a bit more what else you're looking for to accept a PR? I understand that there is the advanced case with a mapper function that stayed unchanged in #17, but it did improve the "basic" use case considerably! I would also argue that when a mapper function is used, the types are probably okay like they are now because they would most likely be the same. On the other hand, if the object contains the promises directly the chances are pretty high (at least for us) that the types are different and it would be very beneficial if the return type would narrow them down correctly. |
@ChristianBoehlke are you interested in doing a PR for this? Would be great to get this improvement! |
I was looking for alternatives, and found a few on npmtrends, but none that are recently released, popular and include TS types 🤔
I guess another alternative for fixing the types would be copying this TypeScript into your project, which removes the mapper and ability to use Maps and mimics the internals of import pMap from 'p-map';
export async function promiseProps<
PromisesObject extends Record<string, Promise<unknown>>,
>(promisesObject: PromisesObject) {
return Object.fromEntries(
await pMap(Object.entries(promisesObject), async ([key, promise]) => {
const result = await promise;
return [key, result];
}),
) as {
[Key in keyof PromisesObject]: PromisesObject[Key] extends Promise<
infer Result
>
? Result
: never;
};
} |
Thanks for the ping @karlhorky, and even more for the types code! I've published https://www.npmjs.com/package/promise-all-props 3.0.0 with types. |
@sindresorhus Thanks for 7eb631f 🙌 This was released as part of |
foo
is:I would have expected it to be:
The text was updated successfully, but these errors were encountered: