Skip to content

Commit

Permalink
Improve returned model type for deserialize function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Madeorsk committed Oct 4, 2024
1 parent 62e62f9 commit 576338f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</p>

<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>

## Introduction
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 8 additions & 8 deletions src/Model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export type IdentifierType<Shape extends ModelShape, K extends keyof Shape> = Sh
/**
* 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.
Expand All @@ -62,7 +62,7 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType>
* Deserialize the model.
* @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.
Expand Down Expand Up @@ -93,15 +93,15 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType>
* @param shape Model shape definition.
* @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,
identifier?: Identifier,
): ModelClass<Shape, Identifier>
): ConstructorOf<ModelType>
{
// Get shape entries.
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()
{
Expand Down Expand Up @@ -161,7 +161,7 @@ export function model<Shape extends ModelShape, Identifier extends keyof Shape =
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) => {
// For each defined model property, assigning its deserialized value.
Expand All @@ -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.

return this as Model<Shape, IdentifierType<Shape, Identifier>>;
return this as unknown as ModelType;
}


Expand Down Expand Up @@ -232,5 +232,5 @@ export function model<Shape extends ModelShape, Identifier extends keyof Shape =
return diff; // Return the difference.
}

} as unknown as ModelClass<Shape, Identifier>;
} as unknown as ConstructorOf<ModelType>;
}

0 comments on commit 576338f

Please sign in to comment.