Skip to content

Commit

Permalink
Revert "Revert "Jsonify: Fix handling of tuples (#638)""
Browse files Browse the repository at this point in the history
This reverts commit 9fdac37.
  • Loading branch information
sindresorhus committed Jul 16, 2023
1 parent 3475a02 commit d743202
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
20 changes: 15 additions & 5 deletions source/jsonify.d.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
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 JsonifyTuple<T extends [unknown, ...unknown[]]> = {
[Key in keyof T]: T[Key] extends NotJsonable ? null : Jsonify<T[Key]>;
};
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 FilterJsonableKeys<T extends object> = {
[Key in keyof T]: T[Key] extends NotJsonable ? never : Key;
Expand Down Expand Up @@ -107,7 +117,7 @@ export type Jsonify<T> = IsAny<T> extends true
: T extends []
? []
: T extends [unknown, ...unknown[]]
? JsonifyTuple<T>
? JsonifyList<T>
: T extends ReadonlyArray<infer U>
? Array<U extends NotJsonable ? null : Jsonify<U>>
: T extends object
Expand Down
3 changes: 3 additions & 0 deletions test-d/jsonify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ 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 d743202

Please sign in to comment.