chore: add typed helpers for Object.xxxx utils

This commit is contained in:
Peter Sanderson
2025-07-09 11:52:59 +02:00
committed by Peter Sanderson
parent 9e0c95d36b
commit 0ed58e7cc3
7 changed files with 45 additions and 15 deletions

View File

@@ -54,9 +54,8 @@ export * from './splitStringEveryNCharacters';
export * from './throttler';
export * from './throwError';
export * from './topologicalSort';
export * from './typedObject';
export * from './typedEventEmitter';
export * from './typedObjectFromEntries';
export * from './typedObjectKeys';
export * from './urlToOnion';
export * from './zip';
export * from './removeTrailingSlashes';

View File

@@ -0,0 +1,15 @@
export const typedObjectEntries = <T extends Record<string, unknown>>(
obj: T,
): [keyof T, T[keyof T]][] => Object.entries(obj) as [keyof T, T[keyof T]][];
export function typedObjectFromEntries<T extends readonly (readonly [string, any])[]>(
entries: T,
): { [K in T[number] as K[0]]: K[1] } {
return Object.fromEntries(entries) as any;
}
export const typedObjectKeys = <T extends Record<any, any>>(obj: T): Array<keyof T> =>
Object.keys(obj) as Array<keyof T>;
export const typedObjectValues = <T extends Record<any, any>>(obj: T): Array<T[keyof T]> =>
Object.values(obj) as Array<T[keyof T]>;

View File

@@ -1,5 +0,0 @@
export function typedObjectFromEntries<T extends readonly (readonly [string, any])[]>(
entries: T,
): { [K in T[number] as K[0]]: K[1] } {
return Object.fromEntries(entries) as any;
}

View File

@@ -1,2 +0,0 @@
export const typedObjectKeys = <T extends Record<any, any>>(obj: T): Array<keyof T> =>
Object.keys(obj) as Array<keyof T>;

View File

@@ -1,5 +1,4 @@
import { typedObjectFromEntries } from '../src/typedObjectFromEntries';
import { typedObjectKeys } from '../src/typedObjectKeys';
import { typedObjectFromEntries, typedObjectKeys } from '../src/typedObject';
const map = {
a: 1,

View File

@@ -1,6 +1,15 @@
import { typedObjectKeys } from '../src/typedObjectKeys';
import { typedObjectKeys } from '../src/typedObject';
type AB = { a: number; b: number } | { b: string };
const ab: AB = { b: 'B' };
type AB = { a: 'A'; b: 'B' } | { b: 'BB' };
export const _test: 'b'[] = typedObjectKeys(ab);
type ExpectedType = 'a' | 'b';
let _assertExpectedType: ExpectedType[];
const test1 = typedObjectKeys({ b: 'BB' } satisfies AB);
_assertExpectedType = test1;
const test2 = typedObjectKeys({ a: 'A', b: 'B' } satisfies AB);
_assertExpectedType = test2;
// eslint-disable-next-line no-self-assign
_assertExpectedType = _assertExpectedType;

View File

@@ -0,0 +1,15 @@
import { typedObjectValues } from '../src/typedObject';
type AB = { a: 'A'; b: 'B' } | { b: 'BB' };
type ExpectedType = 'BB' | 'B' | 'A';
let _assertExpectedType: ExpectedType[];
const test1 = typedObjectValues({ b: 'BB' } satisfies AB);
_assertExpectedType = test1;
const test2 = typedObjectValues({ a: 'A', b: 'B' } satisfies AB);
_assertExpectedType = test2;
// eslint-disable-next-line no-self-assign
_assertExpectedType = _assertExpectedType;