Skip to content

Inferencing unknown Type Improvement #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>();
});
});