Skip to content

Commit

Permalink
Revert "Jsonify: Fix handling of tuples (#638)"
Browse files Browse the repository at this point in the history
This reverts commit 605b901.
  • Loading branch information
sindresorhus committed Jul 16, 2023
1 parent c43d48e commit 9fdac37
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 18 deletions.
20 changes: 5 additions & 15 deletions source/jsonify.d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import type {JsonPrimitive, JsonValue} from './basic';
import type {EmptyObject} from './empty-object';
import type {UndefinedToOptional} from './internal';
import type {IsAny} from './is-any';
import type {IsNever} from './is-never';
import type {NegativeInfinity, PositiveInfinity} from './numeric';
import type {TypedArray} from './typed-array';
import type {IsAny} from './is-any';

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

type FilterNonNever<T extends unknown[]> = T extends [infer F, ...infer R]
? IsNever<F> extends true
? FilterNonNever<R>
: [F, ...FilterNonNever<R>]
: IsNever<T[number]> extends true
? []
: T;

// Handles tuples and arrays
type JsonifyList<T extends unknown[]> = T extends [infer F, ...infer R]
? FilterNonNever<[Jsonify<F>, ...JsonifyList<R>]>
: Array<Jsonify<T[number]>>;
type JsonifyTuple<T extends [unknown, ...unknown[]]> = {
[Key in keyof T]: T[Key] extends NotJsonable ? null : Jsonify<T[Key]>;
};

type FilterJsonableKeys<T extends object> = {
[Key in keyof T]: T[Key] extends NotJsonable ? never : Key;
Expand Down Expand Up @@ -117,7 +107,7 @@ export type Jsonify<T> = IsAny<T> extends true
: T extends []
? []
: T extends [unknown, ...unknown[]]
? JsonifyList<T>
? JsonifyTuple<T>
: T extends ReadonlyArray<infer U>
? Array<U extends NotJsonable ? null : Jsonify<U>>
: T extends object
Expand Down
3 changes: 0 additions & 3 deletions test-d/jsonify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,6 @@ expectType<[string, string]>(tupleJson);
declare const tupleRestJson: Jsonify<[string, ...Date[]]>;
expectType<[string, ...string[]]>(tupleRestJson);

declare const tupleStringJson: Jsonify<string[] & ['some value']>;
expectType<['some value']>(tupleStringJson);

// BigInt fails JSON.stringify
declare const bigInt: Jsonify<bigint>;
expectType<never>(bigInt);
Expand Down

0 comments on commit 9fdac37

Please sign in to comment.