Skip to content
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

Inferencing unknown Type Improvement #11

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 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 the primitive properties from metadata", () => {
johnnyhuirilef marked this conversation as resolved.
Show resolved Hide resolved
type actualPrimitives = Primitives<Metadata>;

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

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