Skip to content

feat!: convert Date type to number #18

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 1 commit into from
Feb 4, 2025
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
30 changes: 18 additions & 12 deletions src/Primitives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ type MethodsAndProperties<T> = { [key in keyof T]: T[key] };

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

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

type DateToNumber<T> = T extends Date ? number : T;

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]> }
: T extends unknown
? T
: never;
: T extends Date
? number
: T extends { value: infer U }
? DateToNumber<U>
: T extends Array<{ value: infer U }>
? DateToNumber<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]>;
}
: T extends unknown
? DateToNumber<T>
: never;

export type Primitives<T> = {
[key in keyof Properties<T>]: ValueObjectValue<T[key]>;
Expand Down
14 changes: 13 additions & 1 deletion tests/Primitives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { DeliveryInfo } from "./DeliveryInfo";
import { Learner } from "./Learner";
import { Metadata } from "./Metadata";
import { Product } from "./Product";
import { Step } from "./Step";
import { User } from "./User";
import { Video } from "./Video";

Expand Down Expand Up @@ -63,7 +64,18 @@ describe("Primitives", () => {

type expectedPrimitives = {
readonly active: boolean;
readonly createdAt: Date;
readonly name: string;
};

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

it("should get primitive number type from Date", () => {
type actualPrimitives = Primitives<Step>;

type expectedPrimitives = {
readonly name: string;
readonly publishedAt: number;
};

expectTypeOf<actualPrimitives>().toEqualTypeOf<expectedPrimitives>();
Expand Down
4 changes: 2 additions & 2 deletions tests/Product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import { Primitives } from "../src";
export class Product {
constructor(
public readonly active: boolean,
public readonly createdAt: Date,
public readonly name: string,
) {}

toPrimitives(): Primitives<Product> {
return {
active: this.active,
createdAt: this.createdAt,
name: this.name,
};
}
}
15 changes: 15 additions & 0 deletions tests/Step.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Primitives } from "../src";

export class Step {
constructor(
public readonly name: string,
public readonly publishedAt: Date,
) {}

toPrimitives(): Primitives<Step> {
return {
name: this.name,
publishedAt: this.publishedAt.getTime(),
};
}
}