Skip to content

Commit

Permalink
fix: infer each property type individually (#9)
Browse files Browse the repository at this point in the history
Add type inference for all properties of an object
  • Loading branch information
JavierCane authored Mar 31, 2023
2 parents 3be4da4 + c34f27c commit d718fb3
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 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.5",
"version": "1.0.6",
"description": "Type entity primitives from value objects",
"keywords": [
"typescript",
Expand Down
2 changes: 1 addition & 1 deletion src/Primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type ValueObjectValue<T> = T extends PrimitiveTypes
: T extends Array<infer U>
? Array<ValueObjectValue<U>>
: T extends { [K in keyof Properties<T>]: infer U }
? { [K in keyof Properties<T>]: ValueObjectValue<U> }
? { [K in keyof Properties<T>]: ValueObjectValue<Properties<T>[K]> }
: never;

export type Primitives<T> = {
Expand Down
9 changes: 7 additions & 2 deletions tests/Address.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Primitives } from "../src/Primitives";
import { Primitives } from "../src";
import { City } from "./City";
import { Street } from "./Street";

export class Address {
constructor(readonly street: Street, readonly city: City) {}
constructor(
readonly street: Street,
readonly city: City,
readonly number: number
) {}

toPrimitives(): Primitives<Address> {
return {
street: this.street.value,
city: this.city.value,
number: this.number,
};
}
}
2 changes: 2 additions & 0 deletions tests/Primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe("Primitives", () => {
readonly address: {
readonly city: string;
readonly street: string;
readonly number: number;
};
};

Expand All @@ -49,6 +50,7 @@ describe("Primitives", () => {
readonly addresses: {
readonly city: string;
readonly street: string;
readonly number: number;
}[];
};

Expand Down

0 comments on commit d718fb3

Please sign in to comment.