mirror of
https://github.com/sindresorhus/type-fest.git
synced 2026-01-25 14:57:30 +00:00
Revert "Jsonify: Fix handling of tuples (#638)"
This reverts commit 605b9011726e72c6ff3735ce6c864263ff5bdcb6.
This commit is contained in:
parent
c43d48e454
commit
9fdac37ea5
20
source/jsonify.d.ts
vendored
20
source/jsonify.d.ts
vendored
@ -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
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user