From fa85edafddce1fb30eb4dc3eff6cc23cc685741b Mon Sep 17 00:00:00 2001 From: "Ben Scholzen (DASPRiD)" Date: Tue, 19 Mar 2024 20:51:10 +0100 Subject: [PATCH] fix: adjust typings for relationship deserializers --- src/deserializer.ts | 71 ++++++++++++++++++++++++++------------------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/src/deserializer.ts b/src/deserializer.ts index 92ecfbd..9646b43 100644 --- a/src/deserializer.ts +++ b/src/deserializer.ts @@ -4,27 +4,37 @@ import type { AttributesSchema, DefaultLinks, DefaultMeta, RootLinks } from "./s export type RelationshipType = "one" | "one_nullable" | "many"; -export type RelationshipDeserializer< +export type ReferenceRelationshipDeserializer< + TRelationshipType extends RelationshipType, TResourceType extends string, +> = { + relationshipType: TRelationshipType; + resourceType: TResourceType; +}; + +export type AnyReferenceRelationshipDeserializer = ReferenceRelationshipDeserializer< + RelationshipType, + string +>; + +export type IncludedRelationshipDeserializer< TRelationshipType extends RelationshipType, - TInclude extends AnyResourceDeserializer | undefined, -> = TInclude extends undefined - ? { - relationshipType: TRelationshipType; - resourceType: TResourceType; - } - : { - relationshipType: TRelationshipType; - include: TInclude; - }; + TInclude extends AnyResourceDeserializer, +> = { + relationshipType: TRelationshipType; + include: TInclude; +}; -export type AnyRelationshipDeserializer = RelationshipDeserializer< - string, +export type AnyIncludedRelationshipDeserializer = IncludedRelationshipDeserializer< RelationshipType, - // biome-ignore lint/suspicious/noExplicitAny: required to avoid circular dependency error - ResourceDeserializer | undefined + // biome-ignore lint/suspicious/noExplicitAny: required to avoid circular dependency + any >; +export type AnyRelationshipDeserializer = + | AnyReferenceRelationshipDeserializer + | AnyIncludedRelationshipDeserializer; + export type Relationships = Record; export type ResourceDeserializer< @@ -43,21 +53,22 @@ export type AnyResourceDeserializer = ResourceDeserializer< Relationships | undefined >; -export type InferResourceType = T extends RelationshipDeserializer< - infer U, +export type InferResourceType = T extends ReferenceRelationshipDeserializer< RelationshipType, - AnyResourceDeserializer | undefined + infer U > ? U - : never; -export type InferRelationshipType = T extends RelationshipDeserializer< - string, - infer U, - AnyResourceDeserializer | undefined -> + : T extends IncludedRelationshipDeserializer + ? U extends AnyResourceDeserializer + ? U["type"] + : never + : never; +export type InferRelationshipType = T extends ReferenceRelationshipDeserializer ? U - : never; -export type InferInclude = T extends RelationshipDeserializer + : T extends IncludedRelationshipDeserializer + ? U + : never; +export type InferInclude = T extends IncludedRelationshipDeserializer ? U : never; export type InferType = T extends ResourceDeserializer< @@ -82,10 +93,10 @@ export type InferRelationships = T extends ResourceDeserializer< ? U : never; -type IncludeResult< - TDeserializer extends AnyRelationshipDeserializer, - TInclude extends AnyResourceDeserializer | undefined = InferInclude, -> = TInclude extends AnyResourceDeserializer ? ResourceResult : { id: string }; +type IncludeResult = + TDeserializer extends AnyReferenceRelationshipDeserializer + ? { id: string } + : InferInclude; export type RelationshipResult< TDeserializer extends AnyRelationshipDeserializer,