Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the type-utils package, split code, add some comments and examples #15743

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 1 addition & 3 deletions packages/blockchain-link/src/workers/electrum/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { CustomError } from '@trezor/blockchain-link-types/src/constants/errors'
import { MESSAGES, RESPONSES } from '@trezor/blockchain-link-types/src/constants';
import type { Response } from '@trezor/blockchain-link-types';
import { Message } from '@trezor/blockchain-link-types/src/messages';
import { Without } from '@trezor/type-utils';

import { BaseWorker, CONTEXT, ContextType } from '../baseWorker';
import * as M from './methods';
Expand All @@ -13,9 +14,6 @@ import type { ElectrumClient } from './client/electrum';
type BlockListener = ReturnType<typeof L.blockListener>;
type TxListener = ReturnType<typeof L.txListener>;

// reason:
// https://stackoverflow.com/questions/57103834/typescript-omit-a-property-from-all-interfaces-in-a-union-but-keep-the-union-s#answer-57103940
type Without<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is exactly same as here.

type Request<T> = T extends any
? T & ContextType<ElectrumClient> & { blockListener: BlockListener; txListener: TxListener }
: never;
Expand Down
10 changes: 10 additions & 0 deletions packages/type-utils/src/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Gets the type of the elements in an array.
*
* Example:
* ```
* type A = (number | string)[];
* type E = ArrayElement<A>; // number | string
* ```
*/
export type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType[number];
5 changes: 4 additions & 1 deletion packages/type-utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export * from './utils';
export * from './array';
export * from './overloads';
export * from './timeout';
export * from './utils';
export * from './object';
13 changes: 13 additions & 0 deletions packages/type-utils/src/object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export type GetObjectWithKey<U, K extends PropertyKey> = U extends object
? K extends keyof U
? U
: never
: never;

export type GetObjectWithoutKey<U, K extends PropertyKey> = U extends object
? K extends keyof U
? never
: U
: never;

export type ObjectsOnly<T> = T extends Record<string, unknown> ? T : never;
Loading
Loading