You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the Record module lacks a built-in, type-safe method for renaming keys in an object while preserving full type information in TypeScript. Renaming object keys is a common task in data transformation, but existing approaches often lead to loss of type information or require cumbersome and error-prone workarounds. Developers need a reliable way to rename keys without sacrificing type safety or resorting to manual type definitions.
What is the feature you are proposing to solve the problem?
I propose adding two new utility functions to the Record module:
renameKey: A function that renames a single key in an object.
Signature:
functionrenameKey<TextendsRecord<string|symbol,unknown>,KextendskeyofT,K2extendsstring,>(input: T,key: K,newKey: K2): {[PinExclude<keyofT,K>|K2]: T[PextendsK2 ? K : P]};
Preserving Type Safety: They maintain full type information of the original object, ensuring that the returned object accurately reflects the types.
Convenience: Provide straightforward methods to rename one or multiple keys without manual type definitions.
Flexibility: renameKeys allows for multiple key renames in a single operation, while renameKey is optimized for single key renaming.
What alternatives have you considered?
Several alternatives were considered but found lacking:
Manual Key Renaming with Type Casting:
Drawbacks:
Verbose and error-prone.
Requires manual updates to types, increasing maintenance overhead.
Risk of losing type information or introducing type errors.
Using Existing Utility Functions or Libraries:
Drawbacks:
Third-party libraries may not handle type preservation adequately.
Introduces additional dependencies to the project.
May not align with the project's existing patterns or standards.
Custom Helper Functions in Individual Projects:
Drawbacks:
Leads to code duplication across different projects.
Inconsistent implementations can cause confusion and bugs.
Lacks the optimization and testing that a standardized utility would have.
Extending Types with Mapped Types Manually:
Drawbacks:
Can become complex quickly, especially with deeply nested objects.
Increases cognitive load for developers.
Not practical for dynamic key renaming based on runtime data.
These alternatives either compromise on type safety, increase complexity, or don't provide a reusable and maintainable solution. By adding renameKey and renameKeys to the Record module, we offer a robust, type-safe, and developer-friendly solution to a common problem.
The text was updated successfully, but these errors were encountered:
sromexs
changed the title
Title: Add renameKey and renameKeys functions to the Record module
Add renameKey and renameKeys functions to the Record module
Sep 22, 2024
Hm, the types here aren't as nice as I'd like when there is a collision in renaming.
As an example:
test("name overrides",()=>{constobj={a: 1,b: 2}asconst;constres=renameKeys(obj,{a: "b"}asconst);expect(res).toEqual({b: 2});// type is inferred as { b: 1 | 2 }});test("name overrides 2",()=>{constobj={a: 1,b: 2}asconst;constres=renameKeys(obj,{b: "a"}asconst);expect(res).toEqual({a: 2});// type is inferred as { a: 1 | 2 }});
Unfortunately, there's no way around this as the types are evaluated at compile time, but you could easily reorder the entries in the object at runtime so there's a different evaluation order in the Record.mapKeys call and therefore get a different runtime value result.
What is the problem this feature would solve?
Currently, the
Record
module lacks a built-in, type-safe method for renaming keys in an object while preserving full type information in TypeScript. Renaming object keys is a common task in data transformation, but existing approaches often lead to loss of type information or require cumbersome and error-prone workarounds. Developers need a reliable way to rename keys without sacrificing type safety or resorting to manual type definitions.What is the feature you are proposing to solve the problem?
I propose adding two new utility functions to the
Record
module:renameKey
: A function that renames a single key in an object.renameKeys
: A function that renames multiple keys in an object based on a mapping.These functions solve the problem by:
renameKeys
allows for multiple key renames in a single operation, whilerenameKey
is optimized for single key renaming.What alternatives have you considered?
Several alternatives were considered but found lacking:
Manual Key Renaming with Type Casting:
Using Existing Utility Functions or Libraries:
Custom Helper Functions in Individual Projects:
Extending Types with Mapped Types Manually:
These alternatives either compromise on type safety, increase complexity, or don't provide a reusable and maintainable solution. By adding
renameKey
andrenameKeys
to theRecord
module, we offer a robust, type-safe, and developer-friendly solution to a common problem.The text was updated successfully, but these errors were encountered: