Skip to content

Commit b9f2ee7

Browse files
(new) renameKeys and 0.31.0 bump (#141)
1 parent fc8fee3 commit b9f2ee7

6 files changed

Lines changed: 65 additions & 12 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
node: ['20', '18', '16']
10+
node: ['24', '22', '20']
1111
name: Node ${{ matrix.node }}
1212
steps:
1313
- name: Checkout repository

package-lock.json

Lines changed: 10 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "types-ramda",
3-
"version": "0.30.1",
3+
"version": "0.31.0",
44
"description": "Dedicated types library for ramda",
55
"author": "Harris Miller <harrismillerconsulting@gmail.com>",
66
"contributors": [
@@ -67,7 +67,7 @@
6767
"dox": "^1.0.0",
6868
"eslint": "^8.50.0",
6969
"eslint-plugin-import": "^2.28.1",
70-
"ramda": "^0.30.1",
70+
"ramda": "^0.31.3",
7171
"rimraf": "^5.0.5",
7272
"tsd": "^0.31.0",
7373
"typescript": "^5.2.2",

test/renameKeys.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { expectAssignable, expectType } from 'tsd';
2+
3+
import { __, renameKeys } from '../es';
4+
5+
expectType<{ bar: number; other: string }>(renameKeys({ foo: 'bar' })({ foo: 123, other: 'abc' }));
6+
expectType<{ foo: number; blah: string }>(renameKeys({ other: 'blah' })({ foo: 123, other: 'abc' }));
7+
8+
expectType<{ bar: number; other: string }>(renameKeys(__, { foo: 123, other: 'abc' })({ foo: 'bar' }));
9+
expectType<{ foo: number; blah: string }>(renameKeys(__, { foo: 123, other: 'abc' })({ other: 'blah' }));
10+
11+
expectType<{ bar: number; other: string }>(renameKeys({ foo: 'bar' }, { foo: 123, other: 'abc' }));
12+
expectType<{ foo: number; blah: string }>(renameKeys({ other: 'blah' }, { foo: 123, other: 'abc' }));
13+
14+
const nonConstObj = { foo: 'bar' };
15+
16+
const what = renameKeys(nonConstObj, { foo: 123, other: 'abc' });
17+
// ^?
18+
// @ts-expect-error - I have no idea why declaring the type throws an error, but it can be calculated above
19+
expectAssignable<{ [x: string]: number; other: string; }>(what);

types/renameKeys.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Placeholder, Prettify, Remap } from './util/tools';
2+
3+
// renameKeys(mapping)(obj)
4+
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>>;
5+
// renameKeys(__, obj)(mapping)
6+
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>>;
7+
// renameKeys(mapping, obj)
8+
export function renameKeys<T, const U extends Partial<Record<keyof T, string>>>(mapping: U, obj: T): Prettify<Omit<T, keyof U> & Remap<T, U>>;

types/util/tools.d.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,3 +520,28 @@ export type NonEmptyArray<T> = [T, ...T[]];
520520
* <created by @harris-miller>
521521
*/
522522
export type ReadonlyNonEmptyArray<T> = readonly [T, ...T[]];
523+
524+
/**
525+
* Prettify type into an object literal
526+
* <created by @harris-miller>
527+
*/
528+
export type Prettify<T> = {[KeyType in keyof T]: T[KeyType]} & {};
529+
530+
type TuplesFromObject<T> = {
531+
[P in keyof T]: [P, T[P]];
532+
}[keyof T];
533+
534+
type GetKeyByValue<T, V> =
535+
TuplesFromObject<T> extends infer TT
536+
? TT extends [infer P, V]
537+
? P
538+
: never
539+
: never;
540+
541+
/**
542+
* Remap keys of one object to the values of mapping object
543+
* <created by @RobinTail>
544+
*/
545+
export type Remap<T, U extends { [P in keyof T]?: string }> = {
546+
[P in NonNullable<U[keyof U]>]: T[GetKeyByValue<U, P>];
547+
};

0 commit comments

Comments
 (0)