Revert "Jsonify: Fix handling of tuples (#638)"

This reverts commit 605b9011726e72c6ff3735ce6c864263ff5bdcb6.
This commit is contained in:
Sindre Sorhus 2023-07-16 11:44:27 +02:00
parent c43d48e454
commit 9fdac37ea5
2 changed files with 5 additions and 18 deletions

20
source/jsonify.d.ts vendored
View File

@ -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;
@ -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

View File

@ -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);