Skip to content

Commit

Permalink
fix entities types
Browse files Browse the repository at this point in the history
  • Loading branch information
udamir committed Nov 11, 2024
1 parent e985644 commit 72de45c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "youtrack-client",
"description": "Client library for accessing the YouTrack REST and Widget API",
"version": "0.3.8",
"version": "0.4.0",
"type": "module",
"keywords": [
"youtrack",
Expand Down
48 changes: 46 additions & 2 deletions src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,50 @@ export type MergeType<T> = (T extends any ? (x: T) => void : never) extends (x:
? { [K in keyof U]: U[K] }
: never

/**
* ExcludeNever<T>
*
* This recursive utility type filters out union members in `T` that contain only properties with the `never` type.
* It is particularly useful for removing deeply nested structures where all fields are `never`,
* effectively cleaning up types with irrelevant `never` branches.
*
* @template T - The type to process, which may include union members, nested objects, or primitive types.
*
* Example:
* type A = {
* a: {
* aa: {
* aaa: number;
* };
* };
* } | {
* a: never;
* } | {
* a: {
* aa: never;
* };
* };
*
* type Result = ExcludeNever<A>;
* // Result is now:
* // {
* // a: {
* // aa: {
* // aaa: number;
* // };
* // };
* // }
*/
export type ExcludeNever<T> = T extends object
? {
[K in keyof T]: ExcludeNever<T[K]>
} extends infer O
? { [K in keyof O]: O[K] } extends Record<string, never>
? never
: O
: never
: T

/**
* FilterFields<T, F> is a utility type that filters fields of object type T based on a given field schema F.
* F can be a mix of flat fields (strings) and nested fields (objects).
Expand Down Expand Up @@ -147,8 +191,8 @@ type FilterObjectFields<T, F extends Record<string, FieldsSchema>> = {
: never
}

export type FilterFields<T, F extends FieldsSchema> = MergeType<
FilterStringFields<T, FilterKeys<F>> | FilterObjectFields<T, FilterNested<F>>
export type FilterFields<T, F extends FieldsSchema> = ExcludeNever<
T extends any ? MergeType<FilterStringFields<T, FilterKeys<F>> | FilterObjectFields<T, FilterNested<F>>> : never
>

/**
Expand Down

0 comments on commit 72de45c

Please sign in to comment.