From 576338fa62d923c007e7816c248fdcd7109df1f1 Mon Sep 17 00:00:00 2001
From: Madeorsk
Date: Fri, 4 Oct 2024 17:33:09 +0200
Subject: [PATCH] Improve returned model type for deserialize function.
---
README.md | 2 +-
package.json | 2 +-
src/Model/Model.ts | 16 ++++++++--------
3 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
index cc54130..0385eeb 100644
--- a/README.md
+++ b/README.md
@@ -19,7 +19,7 @@
-
+
## Introduction
diff --git a/package.json b/package.json
index ce7280b..8a7e0cf 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
{
"name": "@sharkitek/core",
- "version": "3.0.1",
+ "version": "3.0.2",
"description": "TypeScript library for well-designed model architectures.",
"keywords": [
"deserialization",
diff --git a/src/Model/Model.ts b/src/Model/Model.ts
index 0fd40f0..9253549 100644
--- a/src/Model/Model.ts
+++ b/src/Model/Model.ts
@@ -47,7 +47,7 @@ export type IdentifierType = Sh
/**
* Interface of a Sharkitek model definition.
*/
-export interface ModelDefinition
+export interface ModelDefinition = Model>
{
/**
* Get model identifier.
@@ -62,7 +62,7 @@ export interface ModelDefinition
* Deserialize the model.
* @param obj Serialized object.
*/
- deserialize(obj: SerializedModel): Model;
+ deserialize(obj: SerializedModel): ModelType;
/**
* Find out if the model is new (never deserialized) or not.
@@ -93,15 +93,15 @@ export interface ModelDefinition
* @param shape Model shape definition.
* @param identifier Identifier property name.
*/
-export function model(
+export function model>, Shape extends ModelShape, Identifier extends keyof Shape = any>(
shape: Shape,
identifier?: Identifier,
-): ModelClass
+): ConstructorOf
{
// Get shape entries.
const shapeEntries = Object.entries(shape) as [keyof Shape, UnknownDefinition][];
- return class GenericModel implements ModelDefinition>
+ return class GenericModel implements ModelDefinition, ModelType>
{
constructor()
{
@@ -161,7 +161,7 @@ export function model; // Returning the serialized object.
}
- deserialize(obj: SerializedModel): Model>
+ deserialize(obj: SerializedModel): ModelType
{
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, assigning its deserialized value.
@@ -173,7 +173,7 @@ export function model>;
+ return this as unknown as ModelType;
}
@@ -232,5 +232,5 @@ export function model;
+ } as unknown as ConstructorOf;
}