Skip to content

Commit dcf7c10

Browse files
committed
Use type-assertion functions to work around issues with tsc inference.
1 parent 017ca7b commit dcf7c10

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

lib/ecmascript.ts

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3617,36 +3617,37 @@ export function UnbalanceDurationRelative(
36173617
case 'month':
36183618
{
36193619
if (!calendar) throw new RangeError('a starting point is required for months balancing');
3620+
assertExists(relativeTo);
36203621
// balance years down to months
36213622
const dateAdd = calendar.dateAdd;
36223623
const dateUntil = calendar.dateUntil;
3623-
let relativeToDateOnly: Temporal.PlainDateLike = relativeTo as Temporal.PlainDateLike;
36243624
while (MathAbs(years) > 0) {
3625-
const newRelativeTo = CalendarDateAdd(calendar, relativeToDateOnly, oneYear, undefined, dateAdd);
3625+
const newRelativeTo = CalendarDateAdd(calendar, relativeTo, oneYear, undefined, dateAdd);
36263626
const untilOptions = ObjectCreate(null);
36273627
untilOptions.largestUnit = 'month';
3628-
const untilResult = CalendarDateUntil(calendar, relativeToDateOnly, newRelativeTo, untilOptions, dateUntil);
3628+
const untilResult = CalendarDateUntil(calendar, relativeTo, newRelativeTo, untilOptions, dateUntil);
36293629
const oneYearMonths = GetSlot(untilResult, MONTHS);
3630-
relativeToDateOnly = newRelativeTo;
3630+
relativeTo = newRelativeTo;
36313631
months += oneYearMonths;
36323632
years -= sign;
36333633
}
36343634
}
36353635
break;
36363636
case 'week':
36373637
if (!calendar) throw new RangeError('a starting point is required for weeks balancing');
3638+
assertExists(relativeTo);
36383639
// balance years down to days
36393640
while (MathAbs(years) > 0) {
36403641
let oneYearDays;
3641-
({ relativeTo, days: oneYearDays } = MoveRelativeDate(calendar, relativeTo as Temporal.PlainDate, oneYear));
3642+
({ relativeTo, days: oneYearDays } = MoveRelativeDate(calendar, relativeTo, oneYear));
36423643
days += oneYearDays;
36433644
years -= sign;
36443645
}
36453646

36463647
// balance months down to days
36473648
while (MathAbs(months) > 0) {
36483649
let oneMonthDays;
3649-
({ relativeTo, days: oneMonthDays } = MoveRelativeDate(calendar, relativeTo as Temporal.PlainDate, oneMonth));
3650+
({ relativeTo, days: oneMonthDays } = MoveRelativeDate(calendar, relativeTo, oneMonth));
36503651
days += oneMonthDays;
36513652
months -= sign;
36523653
}
@@ -3655,26 +3656,29 @@ export function UnbalanceDurationRelative(
36553656
// balance years down to days
36563657
while (MathAbs(years) > 0) {
36573658
if (!calendar) throw new RangeError('a starting point is required for balancing calendar units');
3659+
assertExists(relativeTo);
36583660
let oneYearDays;
3659-
({ relativeTo, days: oneYearDays } = MoveRelativeDate(calendar, relativeTo as Temporal.PlainDate, oneYear));
3661+
({ relativeTo, days: oneYearDays } = MoveRelativeDate(calendar, relativeTo, oneYear));
36603662
days += oneYearDays;
36613663
years -= sign;
36623664
}
36633665

36643666
// balance months down to days
36653667
while (MathAbs(months) > 0) {
36663668
if (!calendar) throw new RangeError('a starting point is required for balancing calendar units');
3669+
assertExists(relativeTo);
36673670
let oneMonthDays;
3668-
({ relativeTo, days: oneMonthDays } = MoveRelativeDate(calendar, relativeTo as Temporal.PlainDate, oneMonth));
3671+
({ relativeTo, days: oneMonthDays } = MoveRelativeDate(calendar, relativeTo, oneMonth));
36693672
days += oneMonthDays;
36703673
months -= sign;
36713674
}
36723675

36733676
// balance weeks down to days
36743677
while (MathAbs(weeks) > 0) {
36753678
if (!calendar) throw new RangeError('a starting point is required for balancing calendar units');
3679+
assertExists(relativeTo);
36763680
let oneWeekDays;
3677-
({ relativeTo, days: oneWeekDays } = MoveRelativeDate(calendar, relativeTo as Temporal.PlainDate, oneWeek));
3681+
({ relativeTo, days: oneWeekDays } = MoveRelativeDate(calendar, relativeTo, oneWeek));
36783682
days += oneWeekDays;
36793683
weeks -= sign;
36803684
}
@@ -3713,9 +3717,8 @@ export function BalanceDurationRelative(
37133717

37143718
switch (largestUnit) {
37153719
case 'year': {
3716-
// These conditions are effectively identical (as relativeTo being defined forces calendar to be defined)
3717-
// but checking for their truthiness here helps TypeScript and avoids the need for casts below.
3718-
if (!relativeTo || !calendar) throw new RangeError('a starting point is required for years balancing');
3720+
if (!calendar) throw new RangeError('a starting point is required for years balancing');
3721+
assertExists(relativeTo);
37193722
// balance days up to years
37203723
let newRelativeTo, oneYearDays;
37213724
({ relativeTo: newRelativeTo, days: oneYearDays } = MoveRelativeDate(calendar, relativeTo, oneYear));
@@ -3757,9 +3760,8 @@ export function BalanceDurationRelative(
37573760
break;
37583761
}
37593762
case 'month': {
3760-
// These conditions are effectively identical (as relativeTo being defined forces calendar to be defined)
3761-
// but checking for their truthiness here helps TypeScript and avoids the need for casts below.
3762-
if (!relativeTo || !calendar) throw new RangeError('a starting point is required for months balancing');
3763+
if (!calendar) throw new RangeError('a starting point is required for months balancing');
3764+
assertExists(relativeTo);
37633765
// balance days up to months
37643766
let newRelativeTo, oneMonthDays;
37653767
({ relativeTo: newRelativeTo, days: oneMonthDays } = MoveRelativeDate(calendar, relativeTo, oneMonth));
@@ -3772,9 +3774,8 @@ export function BalanceDurationRelative(
37723774
break;
37733775
}
37743776
case 'week': {
3775-
// These conditions are effectively identical (as relativeTo being defined forces calendar to be defined)
3776-
// but checking for their truthiness here helps TypeScript and avoids the need for casts below.
3777-
if (!relativeTo || !calendar) throw new RangeError('a starting point is required for weeks balancing');
3777+
if (!calendar) throw new RangeError('a starting point is required for weeks balancing');
3778+
assertExists(relativeTo);
37783779
// balance days up to weeks
37793780
let newRelativeTo, oneWeekDays;
37803781
({ relativeTo: newRelativeTo, days: oneWeekDays } = MoveRelativeDate(calendar, relativeTo, oneWeek));
@@ -5711,9 +5712,8 @@ export function RoundDuration(
57115712
let total: number;
57125713
switch (unit) {
57135714
case 'year': {
5714-
// These conditions are effectively identical (as relativeTo being defined forces calendar to be defined)
5715-
// but checking for their truthiness here helps TypeScript and avoids the need for casts below.
5716-
if (!relativeTo || !calendar) throw new RangeError('A starting point is required for years rounding');
5715+
if (!calendar) throw new RangeError('A starting point is required for years rounding');
5716+
assertExists(relativeTo);
57175717

57185718
// convert months and weeks to days by calculating difference(
57195719
// relativeTo + years, relativeTo + { years, months, weeks })
@@ -5765,9 +5765,8 @@ export function RoundDuration(
57655765
break;
57665766
}
57675767
case 'month': {
5768-
// These conditions are effectively identical (as relativeTo being defined forces calendar to be defined)
5769-
// but checking for their truthiness here helps TypeScript and avoids the need for casts below.
5770-
if (!relativeTo || !calendar) throw new RangeError('A starting point is required for months rounding');
5768+
if (!calendar) throw new RangeError('A starting point is required for months rounding');
5769+
assertExists(relativeTo);
57715770

57725771
// convert weeks to days by calculating difference(relativeTo +
57735772
// { years, months }, relativeTo + { years, months, weeks })
@@ -5812,9 +5811,8 @@ export function RoundDuration(
58125811
break;
58135812
}
58145813
case 'week': {
5815-
// These conditions are effectively identical (as relativeTo being defined forces calendar to be defined)
5816-
// but checking for their truthiness here helps TypeScript and avoids the need for casts below.
5817-
if (!relativeTo || !calendar) throw new RangeError('A starting point is required for weeks rounding');
5814+
if (!calendar) throw new RangeError('A starting point is required for weeks rounding');
5815+
assertExists(relativeTo);
58185816
// Weeks may be different lengths of days depending on the calendar,
58195817
// convert days to weeks in a loop as described above under 'years'.
58205818
const sign = MathSign(days);

lib/plaindate.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ export class PlainDate implements Temporal.PlainDate {
248248
nanosecond = 0;
249249
if (temporalTime !== undefined) {
250250
temporalTime = ES.ToTemporalTime(temporalTime);
251+
ES.uncheckedAssertNarrowedType<Temporal.ZonedDateTime>(
252+
temporalTime,
253+
'ToTemporalTime above always returns a PlainTime'
254+
);
251255
hour = GetSlot(temporalTime, ISO_HOUR);
252256
minute = GetSlot(temporalTime, ISO_MINUTE);
253257
second = GetSlot(temporalTime, ISO_SECOND);

0 commit comments

Comments
 (0)