From 6bf1606e29b9c7c9248e7e0bdd419f44dd5ddb78 Mon Sep 17 00:00:00 2001 From: Alexander Ryzhikov Date: Sun, 29 Oct 2023 06:42:19 +0200 Subject: [PATCH] fix: fix type imports --- src/type-utils.ts | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/type-utils.ts b/src/type-utils.ts index a946a1f..0323d84 100644 --- a/src/type-utils.ts +++ b/src/type-utils.ts @@ -1,10 +1,14 @@ -import { PositiveInfinity, NegativeInfinity } from 'type-fest/source/numeric'; -import { JsonPrimitive, JsonValue } from 'type-fest/source/basic'; -import { EmptyObject } from 'type-fest/source/empty-object'; -import { TypedArray } from 'type-fest/source/typed-array'; -import { JsonifyList } from 'type-fest/source/jsonify'; -import { WritableDeep } from 'type-fest/source/writable-deep'; - +import type { + PositiveInfinity, + NegativeInfinity, + JsonPrimitive, + JsonValue, + EmptyObject, + TypedArray, + WritableDeep, + IsNever, + IsUnknown, +} from 'type-fest'; export type Id = T extends infer U ? { [K in keyof U]: U[K] } : never; export type Get = P extends keyof T ? T[P] : never; export type Get2 = Get, P2>; @@ -14,6 +18,17 @@ type IsNotJsonableError = Invalid<`${Extract} is not Json-like`> & type NotJsonable = ((...arguments_: any[]) => any) | undefined | symbol | RegExp | Function; +type NeverToNull = IsNever extends true ? null : T; + +// Handles tuples and arrays +type JsonlikeList = T extends [] + ? [] + : T extends [infer F, ...infer R] + ? [NeverToNull>, ...JsonlikeList] + : IsUnknown extends true + ? [] + : Array>; + // tweaked version of Jsonify from type-fest export type Jsonlike = T extends PositiveInfinity | NegativeInfinity ? null @@ -44,9 +59,9 @@ export type Jsonlike = T extend : T extends [] ? [] : T extends unknown[] - ? JsonifyList + ? JsonlikeList : T extends readonly unknown[] - ? JsonifyList> + ? JsonlikeList, DoNotCastToPrimitive> : T extends object ? { [K in keyof T]: [T[K]] extends [NotJsonable] | [never]