From d2be844147fb0b7700d75f13e17aaf75779431be Mon Sep 17 00:00:00 2001 From: Convly Date: Thu, 25 May 2023 16:27:55 +0200 Subject: [PATCH] Fix Attribute.Any definition --- .../lib/types/core/attributes/common.d.ts | 11 +++--- .../lib/types/core/attributes/media.d.ts | 4 +- .../lib/types/core/attributes/relation.d.ts | 38 +++++++++++++------ .../strapi/lib/types/core/attributes/uid.d.ts | 18 ++++----- .../lib/types/core/attributes/utils.d.ts | 2 +- .../strapi/lib/types/core/schemas/index.d.ts | 2 +- 6 files changed, 45 insertions(+), 30 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 dcebd1f10b..6f495e066f 100644 --- a/packages/core/strapi/lib/types/core/attributes/common.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/common.d.ts @@ -2,7 +2,7 @@ * Strapi custom scalar types */ -import type { Attribute } from '@strapi/strapi'; +import type { Attribute, Common } from '@strapi/strapi'; /** * Setters for the attributes options @@ -46,16 +46,17 @@ export type DefaultTo = { default: T }; export type Any = | Attribute.BigInteger | Attribute.Boolean - | Attribute.Component + | Attribute.Component | Attribute.DateTime | Attribute.Date | Attribute.Decimal | Attribute.DynamicZone | Attribute.Email - | Attribute.Enumeration + | Attribute.Enumeration | Attribute.Float + | Attribute.Integer | Attribute.JSON - | Attribute.Media + | (Attribute.Media | Attribute.Media) | Attribute.Password | Attribute.Relation | Attribute.RichText @@ -63,4 +64,4 @@ export type Any = | Attribute.Text | Attribute.Time | Attribute.Timestamp - | Attribute.UID; + | (Attribute.UID | Attribute.UID); diff --git a/packages/core/strapi/lib/types/core/attributes/media.d.ts b/packages/core/strapi/lib/types/core/attributes/media.d.ts index 7f527f6868..8d1d639d16 100644 --- a/packages/core/strapi/lib/types/core/attributes/media.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/media.d.ts @@ -4,7 +4,7 @@ export type AllowedMediaTypes = 'images' | 'videos' | 'files' | 'audios'; export interface MediaProperties< // Media Type - T extends AllowedMediaTypes = undefined, + T extends AllowedMediaTypes | undefined = undefined, // Multiple U extends boolean = false > { @@ -14,7 +14,7 @@ export interface MediaProperties< export type Media< // Media Type - T extends AllowedMediaTypes = undefined, + T extends AllowedMediaTypes | undefined = undefined, // Multiple U extends boolean = false > = Attribute.Attribute<'media'> & diff --git a/packages/core/strapi/lib/types/core/attributes/relation.d.ts b/packages/core/strapi/lib/types/core/attributes/relation.d.ts index f7f2c5f6ca..48a8ba4a9a 100644 --- a/packages/core/strapi/lib/types/core/attributes/relation.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/relation.d.ts @@ -1,27 +1,41 @@ -import type { Attribute, Common } from '@strapi/strapi'; +import type { Attribute, Common, Utils } from '@strapi/strapi'; -export type BasicRelationsType = 'oneToOne' | 'oneToMany' | 'manyToOne' | 'manyToMany'; -export type PolymorphicRelationsType = 'morphToOne' | 'morphToMany' | 'morphOne' | 'morphMany'; +export type BasicRelationsType = + | 'oneToOne' + | 'oneToMany' + | 'manyToOne' + | 'manyToMany' + | 'morphOne' + | 'morphMany'; +export type PolymorphicRelationsType = 'morphToOne' | 'morphToMany'; export type RelationsType = BasicRelationsType | PolymorphicRelationsType; -export interface BasicRelationProperties< +export type BasicRelationProperties< S extends Common.UID.Schema, - R extends RelationsType, + R extends BasicRelationsType, T extends Common.UID.Schema -> { +> = { relation: R; target: T; - inversedBy?: RelationsKeysFromTo; - mappedBy?: RelationsKeysFromTo; -} +} & R extends `morph${string}` + ? { + morphBy?: Utils.KeysBy< + Common.Schemas[T]['attributes'], + Attribute.Relation + >; + } + : { + inversedBy?: RelationsKeysFromTo; + mappedBy?: RelationsKeysFromTo; + }; -export interface PolymorphicRelationProperties { +export interface PolymorphicRelationProperties { relation: R; } export type Relation< - S extends Common.UID.Schema, - R extends RelationsType, + S extends Common.UID.Schema = Common.UID.Schema, + R extends RelationsType = RelationsType, T extends R extends PolymorphicRelationsType ? never : Common.UID.Schema = never > = Attribute.Attribute<'relation'> & // Properties 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 26b0ba031b..85ab92335b 100644 --- a/packages/core/strapi/lib/types/core/attributes/uid.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/uid.d.ts @@ -10,25 +10,21 @@ export interface UIDOptions { export interface UIDProperties< // Own Schema Reference - T extends Common.UID.Schema | undefined = undefined, + T extends Common.UID.Schema | undefined, // Target attribute - U extends T extends Common.UID.Schema - ? Attribute.GetKeysByType - : undefined = undefined, + U extends T extends Common.UID.Schema ? Attribute.GetKeysByType : undefined, // UID options S extends UIDOptions = UIDOptions > { - targetField?: U; - options?: UIDOptions & S; + targetField: U; + options: UIDOptions & S; } export type UID< // Own Schema Reference T extends Common.UID.Schema | undefined = undefined, // Target attribute - U extends T extends Common.UID.Schema - ? Attribute.GetKeysByType - : undefined = undefined, + U extends TargetAttributeByUID = TargetAttributeByUID, // UID options S extends UIDOptions = UIDOptions > = Attribute.Attribute<'uid'> & @@ -41,6 +37,10 @@ export type UID< Attribute.PrivateOption & Attribute.RequiredOption; +type TargetAttributeByUID = T extends Common.UID.Schema + ? Attribute.GetKeysByType + : undefined; + export type UIDValue = string; export type GetUIDValue = T extends UID diff --git a/packages/core/strapi/lib/types/core/attributes/utils.d.ts b/packages/core/strapi/lib/types/core/attributes/utils.d.ts index 774fc388cc..9184381ddf 100644 --- a/packages/core/strapi/lib/types/core/attributes/utils.d.ts +++ b/packages/core/strapi/lib/types/core/attributes/utils.d.ts @@ -1,4 +1,4 @@ -import { Attribute, Common, Utils } from '@strapi/strapi'; +import type { Attribute, Common, Utils } from '@strapi/strapi'; export type PickTypes = T; diff --git a/packages/core/strapi/lib/types/core/schemas/index.d.ts b/packages/core/strapi/lib/types/core/schemas/index.d.ts index d96739932d..c23e4cebf4 100644 --- a/packages/core/strapi/lib/types/core/schemas/index.d.ts +++ b/packages/core/strapi/lib/types/core/schemas/index.d.ts @@ -78,7 +78,7 @@ export interface Info { /** * Low level data structure referencing every schema attribute and its name */ -export interface Attributes extends Utils.StringRecord {} +export interface Attributes extends Utils.StringRecord {} /** * Structure containing every core schema options and their associated value