Skip to content
Open
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
30 changes: 30 additions & 0 deletions src/MutablePrimitives.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* eslint-disable @typescript-eslint/ban-types */
type Methods<T> = {
[P in keyof T]: T[P] extends Function ? P : never;
}[keyof T];

type MethodsAndProperties<T> = { [key in keyof T]: T[key] };

type NonReadonly<T> = {
-readonly [P in keyof T]: T[P];
};

type Properties<T> = Omit<MethodsAndProperties<NonReadonly<T>>, Methods<T>>;

type PrimitiveTypes = string | number | boolean | Date | undefined | null;

type ValueObjectValue<T> = T extends PrimitiveTypes
? T
: T extends { value: infer U }
? U
: T extends Array<{ value: infer U }>
? U[]
: T extends Array<infer U>
? Array<ValueObjectValue<U>>
: T extends { [K in keyof Properties<T>]: unknown }
? { [K in keyof Properties<T>]: ValueObjectValue<Properties<T>[K]> }
: never;

export type MutablePrimitives<T> = {
[key in keyof Properties<T>]: ValueObjectValue<T[key]>;
};
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MutablePrimitives } from "./MutablePrimitives";
import { Primitives } from "./Primitives";

export { Primitives };
export { MutablePrimitives, Primitives };
82 changes: 82 additions & 0 deletions tests/MutablePrimitives.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { expectTypeOf } from "expect-type";

import { MutablePrimitives } from "../src";
import { Course } from "./Course";
import { DeliveryInfo } from "./DeliveryInfo";
import { Learner } from "./Learner";
import { Product } from "./Product";
import { User } from "./User";
import { Video } from "./Video";

describe("MutablePrimitives", () => {
it("should ensure to only return primitive properties excluding methods", () => {
type actualPrimitives = MutablePrimitives<Course>;

type expectedPrimitives = {
courseId: string;
};

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

it("should get primitive array type from value object array", () => {
type actualPrimitives = MutablePrimitives<Learner>;

type expectedPrimitives = {
enrolledCourses: string[];
};

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

it("should generate nested primitive object", () => {
type actualPrimitives = MutablePrimitives<User>;

type expectedPrimitives = {
address: {
city: string;
street: string;
number: number;
};
};

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

it("should generate nested primitive type from array of value objects prop", () => {
type actualPrimitives = MutablePrimitives<DeliveryInfo>;

type expectedPrimitives = {
addresses: {
city: string;
street: string;
number: number;
}[];
};

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

it("should get primitive type in case it is not a value object", () => {
type actualPrimitives = MutablePrimitives<Product>;

type expectedPrimitives = {
active: boolean;
createdAt: Date;
};

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

it("should infer the optional properties", () => {
type actualPrimitives = MutablePrimitives<Video>;

type expectedPrimitives = {
id: string;
name: string | undefined;
duration: number | undefined;
};

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