Skip to content

Commit 4a0d026

Browse files
committed
Minor DRY fix for RoundDuration
Port of tc39/proposal-temporal#1968 Also added a minor type safety cast that I missed when reviewing an earlier PR.
1 parent 0ee4581 commit 4a0d026

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

lib/ecmascript.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,17 +4681,15 @@ export function RoundDuration(
46814681
let nanoseconds = bigInt(nanosecondsParam);
46824682
const TemporalDuration = GetIntrinsic('%Temporal.Duration%');
46834683
let calendar, zdtRelative;
4684-
// if it's a ZDT, it will be reassigned to a PDT below.
4685-
let relativeTo: Parameters<typeof MoveRelativeDate>[1] = relativeToParam;
4686-
if (relativeToParam) {
4684+
// A cast is used below because relativeTo will be either PlainDate or
4685+
// undefined for the rest of this long method (after any ZDT=>PlainDate
4686+
// conversion below), and TS isn't smart enough to know that the type has
4687+
// changed. See https://github.com/microsoft/TypeScript/issues/27706.
4688+
let relativeTo = relativeToParam as Temporal.PlainDate | undefined;
4689+
if (relativeTo) {
46874690
if (IsTemporalZonedDateTime(relativeTo)) {
46884691
zdtRelative = relativeTo;
4689-
const pdt = BuiltinTimeZoneGetPlainDateTimeFor(
4690-
GetSlot(relativeTo, TIME_ZONE),
4691-
GetSlot(relativeTo, INSTANT),
4692-
GetSlot(relativeTo, CALENDAR)
4693-
);
4694-
relativeTo = TemporalDateTimeToDate(pdt);
4692+
relativeTo = ToTemporalDate(relativeTo);
46954693
} else if (!IsTemporalDate(relativeTo)) {
46964694
throw new TypeError('starting point must be PlainDate or ZonedDateTime');
46974695
}

0 commit comments

Comments
 (0)