Skip to content

Commit

Permalink
Jsonify: Remove redundant conditions (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa authored Nov 18, 2023
1 parent 80e454b commit fe03e0a
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions source/jsonify.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import type {IsNever} from './is-never';
import type {IsUnknown} from './is-unknown';
import type {NegativeInfinity, PositiveInfinity} from './numeric';
import type {TypedArray} from './typed-array';
import type {WritableDeep} from './writable-deep';
import type {UnknownArray} from './unknown-array';

// Note: The return value has to be `any` and not `unknown` so it can match `void`.
type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol;

type NeverToNull<T> = IsNever<T> extends true ? null : T;

// Handles tuples and arrays
type JsonifyList<T extends readonly unknown[]> = T extends []
type JsonifyList<T extends UnknownArray> = T extends readonly []
? []
: T extends [infer F, ...infer R]
: T extends readonly [infer F, ...infer R]
? [NeverToNull<Jsonify<F>>, ...JsonifyList<R>]
: IsUnknown<T[number]> extends true
? []
Expand Down Expand Up @@ -114,12 +114,8 @@ export type Jsonify<T> = IsAny<T> extends true
? Record<string, number>
: T extends NotJsonable
? never // Non-JSONable type union was found not empty
: T extends []
? []
: T extends unknown[]
? JsonifyList<T>
: T extends readonly unknown[]
? JsonifyList<WritableDeep<T>>
: T extends object
? JsonifyObject<UndefinedToOptional<T>> // JsonifyObject recursive call for its children
: never; // Otherwise any other non-object is removed
: T extends UnknownArray
? JsonifyList<T>
: T extends object
? JsonifyObject<UndefinedToOptional<T>> // JsonifyObject recursive call for its children
: never; // Otherwise any other non-object is removed

0 comments on commit fe03e0a

Please sign in to comment.