From 25d521e3f1de045d26d9399b4dfb9014de3f7e09 Mon Sep 17 00:00:00 2001 From: Convly Date: Fri, 4 Aug 2023 08:46:54 +0200 Subject: [PATCH] Remove the targetField type narrowing causing circular dep --- .../lib/types/core/attributes/common.d.ts | 12 ++++++++- .../strapi/lib/types/core/attributes/uid.d.ts | 27 +++++++------------ .../strapi/lib/types/utils/expression.d.ts | 10 +++++++ .../core/strapi/lib/types/utils/guard.d.ts | 15 ++++++++++- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/packages/core/strapi/lib/types/core/attributes/common.d.ts b/packages/core/strapi/lib/types/core/attributes/common.d.ts index 39ab454cd9..ac18b037d4 100644 --- a/packages/core/strapi/lib/types/core/attributes/common.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/common.d.ts @@ -71,4 +71,14 @@ export type Any = | Attribute.Text | Attribute.Time | Attribute.Timestamp - | Attribute.UID; + | Attribute.UID; + +export type PopulatableKind = Extract< + Attribute.Kind, + 'relation' | 'component' | 'dynamiczone' | 'media' +>; + +export type NonPopulatableKind = Exclude< + Attribute.Kind, + 'relation' | 'component' | 'dynamiczone' | 'media' +>; diff --git a/packages/core/strapi/lib/types/core/attributes/uid.d.ts b/packages/core/strapi/lib/types/core/attributes/uid.d.ts index 8b962e9da7..da830f74ac 100644 --- a/packages/core/strapi/lib/types/core/attributes/uid.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/uid.d.ts @@ -9,28 +9,26 @@ export interface UIDOptions { } export interface UIDProperties< - TOrigin extends Common.UID.Schema, - TTargetAttribute extends AllowedTargetAttributes, + TTargetAttribute extends string = string, TOptions extends UIDOptions = UIDOptions > { targetField: TTargetAttribute; options: UIDOptions & TOptions; } -export interface GenericUIDProperties { - targetField?: string; - options: TOptions & UIDOptions; -} - +/** + * @param {Common.UID.Schema} [_TOrigin] + */ export type UID< - TOrigin extends Common.UID.Schema | undefined = undefined, - TTargetAttribute extends AllowedTargetAttributes = AllowedTargetAttributes, + // TODO: V5: + // The TOrigin was used to narrow down the list of possible target attribute for a + // UID, but was removed due to circular dependency issues and will be removed in V5 + _TOrigin extends Common.UID.Schema = never, + TTargetAttribute extends string = string, TOptions extends UIDOptions = UIDOptions > = Attribute.OfType<'uid'> & // Properties - (TOrigin extends Common.UID.Schema - ? UIDProperties - : GenericUIDProperties) & + UIDProperties & // Options Attribute.ConfigurableOption & Attribute.DefaultOption & @@ -38,11 +36,6 @@ export type UID< Attribute.PrivateOption & Attribute.RequiredOption; -type AllowedTargetAttributes = - TOrigin extends Common.UID.Schema - ? Utils.Guard.Never, string> - : never; - export type UIDValue = string; export type GetUIDValue = TAttribute extends UID< diff --git a/packages/core/strapi/lib/types/utils/expression.d.ts b/packages/core/strapi/lib/types/utils/expression.d.ts index eceebf2e00..0b4f902c22 100644 --- a/packages/core/strapi/lib/types/utils/expression.d.ts +++ b/packages/core/strapi/lib/types/utils/expression.d.ts @@ -4,6 +4,16 @@ export type True = true; export type False = false; export type BooleanValue = True | False; +export type IsNever = [TValue] extends [never] ? True : False; + +export type IsNotNever = Not>; + +export type IsTrue = [TValue] extends [True] ? True : False; + +export type IsFalse = [TValue] extends [False] ? True : False; + +export type StrictEqual = And, Extends>; + export type Extends = [TLeft] extends [TRight] ? True : False; export type Not = If; diff --git a/packages/core/strapi/lib/types/utils/guard.d.ts b/packages/core/strapi/lib/types/utils/guard.d.ts index 1872d3500f..4b181a6145 100644 --- a/packages/core/strapi/lib/types/utils/guard.d.ts +++ b/packages/core/strapi/lib/types/utils/guard.d.ts @@ -1,3 +1,5 @@ +import type { Utils } from '@strapi/strapi'; + /** * Assign a default value `TDefault` to `TValue` if `TValue` is of type `never` * @@ -11,4 +13,15 @@ * type X = Never * // string */ -export type Never = [TValue] extends [never] ? TDefault : TValue; +export type Never = OfTypes<[never], TValue, TFallback>; + +export type OfTypes = TTypes extends [ + infer THead extends unknown, + ...infer TTail extends unknown[] +] + ? Utils.Expression.If< + Utils.Expression.StrictEqual, + TFallback, + Utils.Expression.If, OfTypes, TValue> + > + : never;