mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-02 21:45:14 +01:00
chore: add typed helpers for Object.xxxx utils
This commit is contained in:
committed by
Peter Sanderson
parent
9e0c95d36b
commit
0ed58e7cc3
@@ -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';
|
||||
|
||||
15
packages/utils/src/typedObject.ts
Normal file
15
packages/utils/src/typedObject.ts
Normal 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]>;
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
export const typedObjectKeys = <T extends Record<any, any>>(obj: T): Array<keyof T> =>
|
||||
Object.keys(obj) as Array<keyof T>;
|
||||
@@ -1,5 +1,4 @@
|
||||
import { typedObjectFromEntries } from '../src/typedObjectFromEntries';
|
||||
import { typedObjectKeys } from '../src/typedObjectKeys';
|
||||
import { typedObjectFromEntries, typedObjectKeys } from '../src/typedObject';
|
||||
|
||||
const map = {
|
||||
a: 1,
|
||||
|
||||
@@ -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;
|
||||
|
||||
15
packages/utils/tests/typedObjectValues.type-test.ts
Normal file
15
packages/utils/tests/typedObjectValues.type-test.ts
Normal 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;
|
||||
Reference in New Issue
Block a user