Skip to content

Commit

Permalink
Improve DateTime type preservation (#4255)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim <[email protected]>
  • Loading branch information
sukovanej and tim-smart authored Jan 14, 2025
1 parent 7b3d58d commit a66c2eb
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 92 deletions.
5 changes: 5 additions & 0 deletions .changeset/fuzzy-roses-rush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Improve DateTime type preservation
91 changes: 91 additions & 0 deletions packages/effect/dtslint/DateTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import * as DateTime from "effect/DateTime"

declare const utc: DateTime.Utc
declare const zoned: DateTime.Zoned
declare const any: DateTime.DateTime

// -------------------------------------------------------------------------------------
// min
// -------------------------------------------------------------------------------------

// $ExpectType Utc | Zoned
DateTime.min(utc, zoned)

// $ExpectType Utc
DateTime.min(utc, utc)

// $ExpectType Zoned
DateTime.min(zoned, zoned)

// $ExpectType DateTime
DateTime.min(any, zoned)

// $ExpectType DateTime
DateTime.min(any, utc)

// $ExpectType DateTime
DateTime.min(any, any)

// $ExpectType Utc | Zoned
utc.pipe(DateTime.min(zoned))

// $ExpectType Utc | Zoned
zoned.pipe(DateTime.min(utc))

// $ExpectType Utc
utc.pipe(DateTime.min(utc))

// $ExpectType Zoned
zoned.pipe(DateTime.min(zoned))

// $ExpectType DateTime
any.pipe(DateTime.min(zoned))

// $ExpectType DateTime
any.pipe(DateTime.min(utc))

// $ExpectType DateTime
any.pipe(DateTime.min(any))

// -------------------------------------------------------------------------------------
// max
// -------------------------------------------------------------------------------------

// $ExpectType Utc | Zoned
DateTime.max(utc, zoned)

// $ExpectType Utc
DateTime.max(utc, utc)

// $ExpectType Zoned
DateTime.max(zoned, zoned)

// $ExpectType DateTime
DateTime.max(any, zoned)

// $ExpectType DateTime
DateTime.max(any, utc)

// $ExpectType DateTime
DateTime.max(any, any)

// $ExpectType Utc | Zoned
utc.pipe(DateTime.max(zoned))

// $ExpectType Utc | Zoned
zoned.pipe(DateTime.max(utc))

// $ExpectType Utc
utc.pipe(DateTime.max(utc))

// $ExpectType Zoned
zoned.pipe(DateTime.max(zoned))

// $ExpectType DateTime
any.pipe(DateTime.max(zoned))

// $ExpectType DateTime
any.pipe(DateTime.max(utc))

// $ExpectType DateTime
any.pipe(DateTime.max(any))
118 changes: 56 additions & 62 deletions packages/effect/src/DateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,13 @@ export const Order: order.Order<DateTime> = Internal.Order
* @since 3.6.0
*/
export const clamp: {
(options: { minimum: DateTime; maximum: DateTime }): (self: DateTime) => DateTime
(self: DateTime, options: { minimum: DateTime; maximum: DateTime }): DateTime
<Min extends DateTime, Max extends DateTime>(
options: { readonly minimum: Min; readonly maximum: Max }
): <A extends DateTime>(self: A) => A | Min | Max
<A extends DateTime, Min extends DateTime, Max extends DateTime>(
self: A,
options: { readonly minimum: Min; readonly maximum: Max }
): A | Min | Max
} = Internal.clamp

// =============================================================================
Expand Down Expand Up @@ -704,17 +709,17 @@ export const distanceDuration: {
* @category comparisons
*/
export const min: {
(that: DateTime): (self: DateTime) => DateTime
(self: DateTime, that: DateTime): DateTime
<That extends DateTime>(that: That): <Self extends DateTime>(self: Self) => Self | That
<Self extends DateTime, That extends DateTime>(self: Self, that: That): Self | That
} = Internal.min

/**
* @since 3.6.0
* @category comparisons
*/
export const max: {
(that: DateTime): (self: DateTime) => DateTime
(self: DateTime, that: DateTime): DateTime
<That extends DateTime>(that: That): <Self extends DateTime>(self: Self) => Self | That
<Self extends DateTime, That extends DateTime>(self: Self, that: That): Self | That
} = Internal.max

/**
Expand Down Expand Up @@ -929,13 +934,8 @@ export const getPart: {
* @category parts
*/
export const setParts: {
(
parts: Partial<DateTime.PartsWithWeekday>
): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(
self: A,
parts: Partial<DateTime.PartsWithWeekday>
): DateTime.PreserveZone<A>
(parts: Partial<DateTime.PartsWithWeekday>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsWithWeekday>): A
} = Internal.setParts

/**
Expand All @@ -945,13 +945,8 @@ export const setParts: {
* @category parts
*/
export const setPartsUtc: {
(
parts: Partial<DateTime.PartsWithWeekday>
): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(
self: A,
parts: Partial<DateTime.PartsWithWeekday>
): DateTime.PreserveZone<A>
(parts: Partial<DateTime.PartsWithWeekday>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsWithWeekday>): A
} = Internal.setPartsUtc

// =============================================================================
Expand Down Expand Up @@ -1127,8 +1122,8 @@ export const nowInCurrentZone: Effect.Effect<Zoned, never, CurrentTimeZone> = Ef
* @category mapping
*/
export const mutate: {
(f: (date: Date) => void): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, f: (date: Date) => void): DateTime.PreserveZone<A>
(f: (date: Date) => void): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, f: (date: Date) => void): A
} = Internal.mutate

/**
Expand All @@ -1138,8 +1133,8 @@ export const mutate: {
* @category mapping
*/
export const mutateUtc: {
(f: (date: Date) => void): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, f: (date: Date) => void): DateTime.PreserveZone<A>
(f: (date: Date) => void): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, f: (date: Date) => void): A
} = Internal.mutateUtc

/**
Expand All @@ -1159,8 +1154,8 @@ export const mutateUtc: {
* ```
*/
export const mapEpochMillis: {
(f: (millis: number) => number): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, f: (millis: number) => number): DateTime.PreserveZone<A>
(f: (millis: number) => number): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, f: (millis: number) => number): A
} = Internal.mapEpochMillis

/**
Expand Down Expand Up @@ -1240,8 +1235,8 @@ export const match: {
* ```
*/
export const addDuration: {
(duration: Duration.DurationInput): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, duration: Duration.DurationInput): DateTime.PreserveZone<A>
(duration: Duration.DurationInput): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, duration: Duration.DurationInput): A
} = Internal.addDuration

/**
Expand All @@ -1260,8 +1255,8 @@ export const addDuration: {
* ```
*/
export const subtractDuration: {
(duration: Duration.DurationInput): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, duration: Duration.DurationInput): DateTime.PreserveZone<A>
(duration: Duration.DurationInput): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, duration: Duration.DurationInput): A
} = Internal.subtractDuration

/**
Expand All @@ -1283,13 +1278,8 @@ export const subtractDuration: {
* ```
*/
export const add: {
(
parts: Partial<DateTime.PartsForMath>
): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(
self: A,
parts: Partial<DateTime.PartsForMath>
): DateTime.PreserveZone<A>
(parts: Partial<DateTime.PartsForMath>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsForMath>): A
} = Internal.add

/**
Expand All @@ -1308,13 +1298,8 @@ export const add: {
* ```
*/
export const subtract: {
(
parts: Partial<DateTime.PartsForMath>
): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(
self: A,
parts: Partial<DateTime.PartsForMath>
): DateTime.PreserveZone<A>
(parts: Partial<DateTime.PartsForMath>): <A extends DateTime>(self: A) => A
<A extends DateTime>(self: A, parts: Partial<DateTime.PartsForMath>): A
} = Internal.subtract

/**
Expand All @@ -1337,12 +1322,15 @@ export const subtract: {
* ```
*/
export const startOf: {
(part: DateTime.UnitSingular, options?: {
readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined
}): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, part: DateTime.UnitSingular, options?: {
readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined
}): DateTime.PreserveZone<A>
(
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): <A extends DateTime>(self: A) => A
<A extends DateTime>(
self: A,
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): A
} = Internal.startOf

/**
Expand All @@ -1365,12 +1353,15 @@ export const startOf: {
* ```
*/
export const endOf: {
(part: DateTime.UnitSingular, options?: {
readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined
}): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, part: DateTime.UnitSingular, options?: {
readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined
}): DateTime.PreserveZone<A>
(
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): <A extends DateTime>(self: A) => A
<A extends DateTime>(
self: A,
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): A
} = Internal.endOf

/**
Expand All @@ -1393,12 +1384,15 @@ export const endOf: {
* ```
*/
export const nearest: {
(part: DateTime.UnitSingular, options?: {
readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined
}): <A extends DateTime>(self: A) => DateTime.PreserveZone<A>
<A extends DateTime>(self: A, part: DateTime.UnitSingular, options?: {
readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined
}): DateTime.PreserveZone<A>
(
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): <A extends DateTime>(self: A) => A
<A extends DateTime>(
self: A,
part: DateTime.UnitSingular,
options?: { readonly weekStartsOn?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | undefined }
): A
} = Internal.nearest

// =============================================================================
Expand Down
Loading

0 comments on commit a66c2eb

Please sign in to comment.