Improve returned model type for deserialize function.

This commit is contained in:
Madeorsk 2024-10-04 17:33:09 +02:00
parent 62e62f962e
commit da3654969e
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
3 changed files with 10 additions and 10 deletions

View file

@ -19,7 +19,7 @@
</p> </p>
<p align="center"> <p align="center">
<img alt="Version 3.0.1" src="https://img.shields.io/badge/version-3.0.1-blue" /> <img alt="Version 3.0.2" src="https://img.shields.io/badge/version-3.0.2-blue" />
</p> </p>
## Introduction ## Introduction

View file

@ -1,6 +1,6 @@
{ {
"name": "@sharkitek/core", "name": "@sharkitek/core",
"version": "3.0.1", "version": "3.0.2",
"description": "TypeScript library for well-designed model architectures.", "description": "TypeScript library for well-designed model architectures.",
"keywords": [ "keywords": [
"deserialization", "deserialization",

View file

@ -47,7 +47,7 @@ export type IdentifierType<Shape extends ModelShape, K extends keyof Shape> = Sh
/** /**
* Interface of a Sharkitek model definition. * Interface of a Sharkitek model definition.
*/ */
export interface ModelDefinition<Shape extends ModelShape, IdentifierType> export interface ModelDefinition<Shape extends ModelShape, IdentifierType, ModelType extends Model<Shape, IdentifierType> = Model<Shape, IdentifierType>>
{ {
/** /**
* Get model identifier. * Get model identifier.
@ -62,7 +62,7 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType>
* Deserialize the model. * Deserialize the model.
* @param obj Serialized object. * @param obj Serialized object.
*/ */
deserialize(obj: SerializedModel<Shape>): Model<Shape, IdentifierType>; deserialize(obj: SerializedModel<Shape>): ModelType;
/** /**
* Find out if the model is new (never deserialized) or not. * Find out if the model is new (never deserialized) or not.
@ -93,15 +93,15 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType>
* @param shape Model shape definition. * @param shape Model shape definition.
* @param identifier Identifier property name. * @param identifier Identifier property name.
*/ */
export function model<Shape extends ModelShape, Identifier extends keyof Shape = any>( export function model<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends keyof Shape = any>(
shape: Shape, shape: Shape,
identifier?: Identifier, identifier?: Identifier,
): ModelClass<Shape, Identifier> ): ConstructorOf<ModelType>
{ {
// Get shape entries. // Get shape entries.
const shapeEntries = Object.entries(shape) as [keyof Shape, UnknownDefinition][]; const shapeEntries = Object.entries(shape) as [keyof Shape, UnknownDefinition][];
return class GenericModel implements ModelDefinition<Shape, IdentifierType<Shape, Identifier>> return class GenericModel implements ModelDefinition<Shape, IdentifierType<Shape, Identifier>, ModelType>
{ {
constructor() constructor()
{ {
@ -161,7 +161,7 @@ export function model<Shape extends ModelShape, Identifier extends keyof Shape =
return serializedObject as SerializedModel<Shape>; // Returning the serialized object. return serializedObject as SerializedModel<Shape>; // Returning the serialized object.
} }
deserialize(obj: SerializedModel<Shape>): Model<Shape, IdentifierType<Shape, Identifier>> deserialize(obj: SerializedModel<Shape>): ModelType
{ {
this.forEachModelProperty((propertyName, propertyDefinition) => { this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, assigning its deserialized value. // For each defined model property, assigning its deserialized value.
@ -173,7 +173,7 @@ export function model<Shape extends ModelShape, Identifier extends keyof Shape =
this._originalObject = obj; // The model is not a new one, but loaded from a deserialized one. Storing it. this._originalObject = obj; // The model is not a new one, but loaded from a deserialized one. Storing it.
return this as Model<Shape, IdentifierType<Shape, Identifier>>; return this as unknown as ModelType;
} }
@ -232,5 +232,5 @@ export function model<Shape extends ModelShape, Identifier extends keyof Shape =
return diff; // Return the difference. return diff; // Return the difference.
} }
} as unknown as ModelClass<Shape, Identifier>; } as unknown as ConstructorOf<ModelType>;
} }