Skip to content

new(rebuild, mapKeys, renameKeys) add types for new functions #140

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

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from

Conversation

Harris-Miller
Copy link
Collaborator

Types for new function added in ramda/ramda#3493

@RobinTail
Copy link
Contributor

RobinTail commented Jun 27, 2025

https://stackoverflow.com/questions/55454125/typescript-remapping-object-properties-in-typesafe

For the needs of my project I came up with this compilation.
Perhaps it could be useful, @Harris-Miller

type TuplesFromObject<T> = {
  [P in keyof T]: [P, T[P]];
}[keyof T];

type GetKeyByValue<T, V> =
  TuplesFromObject<T> extends infer TT
    ? TT extends [infer P, V]
      ? P
      : never
    : never;

export type Remap<T, U extends { [P in keyof T]?: V }, V extends string> = {
  [P in NonNullable<U[keyof U]>]: T[GetKeyByValue<U, P>];
};

export type Intact<T, U> = { [K in Exclude<keyof T, keyof U>]: T[K] };

@Harris-Miller
Copy link
Collaborator Author

Ugh ok this one sucks

@RobinTail thanks for the suggestion, that worked great for renameKeys. Because of how that function takes an object who's type can be deconstructed into a tuple of key-value-pairs, we can extract the new key values to replace the incoming object with

That isn't really possible for rebuild and mapKeys. Those are functions return either entry tuples or dynamic keys without knowing what the original key was it was built from. This means for any object that doesn't have consistent value types breakdown hard

For now, I'm choosing to have those both return Record<string, T> until I can figure something better. That type is the baseline that should allow it to be assigned to predefined types without needing to cast. I might need to test that more first before merge

@Harris-Miller
Copy link
Collaborator Author

Actually, I may update this MR to just add renameKeys and leave the others for another MR. I can release multiple patches for @types/ramda without issue

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

Successfully merging this pull request may close these issues.

2 participants