Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: ['20', '18', '16']
node: ['24', '22', '20']
name: Node ${{ matrix.node }}
steps:
- name: Checkout repository
Expand Down
19 changes: 10 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "types-ramda",
"version": "0.30.1",
"version": "0.31.0",
"description": "Dedicated types library for ramda",
"author": "Harris Miller <harrismillerconsulting@gmail.com>",
"contributors": [
Expand Down Expand Up @@ -67,7 +67,7 @@
"dox": "^1.0.0",
"eslint": "^8.50.0",
"eslint-plugin-import": "^2.28.1",
"ramda": "^0.30.1",
"ramda": "^0.31.3",
"rimraf": "^5.0.5",
"tsd": "^0.31.0",
"typescript": "^5.2.2",
Expand Down
19 changes: 19 additions & 0 deletions test/renameKeys.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expectAssignable, expectType } from 'tsd';

import { __, renameKeys } from '../es';

expectType<{ bar: number; other: string }>(renameKeys({ foo: 'bar' })({ foo: 123, other: 'abc' }));
expectType<{ foo: number; blah: string }>(renameKeys({ other: 'blah' })({ foo: 123, other: 'abc' }));

expectType<{ bar: number; other: string }>(renameKeys(__, { foo: 123, other: 'abc' })({ foo: 'bar' }));
expectType<{ foo: number; blah: string }>(renameKeys(__, { foo: 123, other: 'abc' })({ other: 'blah' }));

expectType<{ bar: number; other: string }>(renameKeys({ foo: 'bar' }, { foo: 123, other: 'abc' }));
expectType<{ foo: number; blah: string }>(renameKeys({ other: 'blah' }, { foo: 123, other: 'abc' }));

const nonConstObj = { foo: 'bar' };

const what = renameKeys(nonConstObj, { foo: 123, other: 'abc' });
// ^?
// @ts-expect-error - I have no idea why declaring the type throws an error, but it can be calculated above
expectAssignable<{ [x: string]: number; other: string; }>(what);
8 changes: 8 additions & 0 deletions types/renameKeys.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Placeholder, Prettify, Remap } from './util/tools';

// renameKeys(mapping)(obj)
export function renameKeys<const U extends Record<PropertyKey, string>>(mapping: U): <T extends Record<keyof U, unknown>>(obj: T) => Prettify<Omit<T, keyof U> & Remap<T, U>>;
// renameKeys(__, obj)(mapping)
export function renameKeys<T>(__: Placeholder, obj: T): <const U extends Partial<Record<keyof T, string>>>(mapping: U) => Prettify<Omit<T, keyof U> & Remap<T, U>>;
// renameKeys(mapping, obj)
export function renameKeys<T, const U extends Partial<Record<keyof T, string>>>(mapping: U, obj: T): Prettify<Omit<T, keyof U> & Remap<T, U>>;
25 changes: 25 additions & 0 deletions types/util/tools.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,3 +520,28 @@ export type NonEmptyArray<T> = [T, ...T[]];
* <created by @harris-miller>
*/
export type ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];

/**
* Prettify type into an object literal
* <created by @harris-miller>
*/
export type Prettify<T> = {[KeyType in keyof T]: T[KeyType]} & {};

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;

/**
* Remap keys of one object to the values of mapping object
* <created by @RobinTail>
*/
export type Remap<T, U extends { [P in keyof T]?: string }> = {
[P in NonNullable<U[keyof U]>]: T[GetKeyByValue<U, P>];
};