Skip to content

Commit

Permalink
feat: add unknown properties support (#11)
Browse files Browse the repository at this point in the history
* test: add Metadata class and test for inferring primitive properties from metadata
* fix: update ValueObjectValue type to handle unknown key properties
* test: refine title test case to infer properties with unknown type
* chore: update package version

Co-authored-by: Javier Ferrer González <javier.ferrer@codely.com>
  • Loading branch information
johnnyhuirilef and JavierCane authored Sep 4, 2024
1 parent f11bad7 commit 617e4df
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@codelytv/primitives-type",
"version": "1.0.6",
"version": "1.1.0",
"description": "Type entity primitives from value objects",
"keywords": [
"typescript",
Expand Down
4 changes: 3 additions & 1 deletion src/Primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ type ValueObjectValue<T> = T extends PrimitiveTypes
? U[]
: T extends Array<infer U>
? Array<ValueObjectValue<U>>
: T extends { [K in keyof Properties<T>]: infer U }
: T extends { [K in keyof Properties<T>]: unknown }
? { [K in keyof Properties<T>]: ValueObjectValue<Properties<T>[K]> }
: T extends unknown
? T
: never;

export type Primitives<T> = {
Expand Down
11 changes: 11 additions & 0 deletions tests/Metadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Primitives } from "../src";

export class Metadata {
constructor(public readonly data: Record<string, unknown>) {}

toPrimitives(): Primitives<Metadata> {
return {
data: this.data,
};
}
}
11 changes: 11 additions & 0 deletions tests/Primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Primitives } from "../src";
import { Course } from "./Course";
import { DeliveryInfo } from "./DeliveryInfo";
import { Learner } from "./Learner";
import { Metadata } from "./Metadata";
import { Product } from "./Product";
import { User } from "./User";
import { Video } from "./Video";
Expand Down Expand Up @@ -79,4 +80,14 @@ describe("Primitives", () => {

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});

it("should infer properties with unknown type", () => {
type actualPrimitives = Primitives<Metadata>;

type expectedPrimitives = {
readonly data: Record<string, unknown>;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
});
});

0 comments on commit 617e4df

Please sign in to comment.