11import { Scope } from "./scope.js" ;
2- import type { Temporal } from "temporal-polyfill" ;
2+
3+ // Temporary workaround to support environments without Temporal
4+ // Replace with Temporal.* types once they are provided by TypeScript
5+ // In addition to this minimal interface, these objects are also expected
6+ // to be supported by Intl.DateTimeFormat
7+ interface TemporalObject {
8+ epochMilliseconds ?: number ;
9+ toZonedDateTime ?( timeZone : string ) : { epochMilliseconds : number } ;
10+ calendarId ?: string ;
11+ toString ( ) : string ;
12+ }
313
414export type FluentValue = FluentType < unknown > | string ;
515
616export type FluentVariable =
717 | FluentValue
8- | Temporal . Instant
9- | Temporal . PlainDateTime
10- | Temporal . PlainDate
11- | Temporal . PlainTime
12- | Temporal . PlainYearMonth
13- | Temporal . PlainMonthDay
18+ | TemporalObject
1419 | string
1520 | number
1621 | Date ;
@@ -125,12 +130,7 @@ export class FluentNumber extends FluentType<number> {
125130export class FluentDateTime extends FluentType <
126131 | number
127132 | Date
128- | Temporal . Instant
129- | Temporal . PlainDateTime
130- | Temporal . PlainDate
131- | Temporal . PlainMonthDay
132- | Temporal . PlainTime
133- | Temporal . PlainYearMonth
133+ | TemporalObject
134134> {
135135 /** Options passed to `Intl.DateTimeFormat`. */
136136 public opts : Intl . DateTimeFormatOptions ;
@@ -141,14 +141,17 @@ export class FluentDateTime extends FluentType<
141141 if ( value instanceof FluentType ) return FluentDateTime . supportsValue ( value . valueOf ( ) ) ;
142142 // Temporary workaround to support environments without Temporal
143143 if ( 'Temporal' in globalThis ) {
144+ // for TypeScript, which doesn't know about Temporal yet
145+ const _Temporal = (
146+ globalThis as unknown as { Temporal : Record < string , ( ) => unknown > }
147+ ) . Temporal ;
144148 if (
145- // @ts -ignore
146- value instanceof Temporal . Instant || // @ts -ignore
147- value instanceof Temporal . PlainDateTime || // @ts -ignore
148- value instanceof Temporal . PlainDate || // @ts -ignore
149- value instanceof Temporal . PlainMonthDay || // @ts -ignore
150- value instanceof Temporal . PlainTime || // @ts -ignore
151- value instanceof Temporal . PlainYearMonth
149+ value instanceof _Temporal . Instant ||
150+ value instanceof _Temporal . PlainDateTime ||
151+ value instanceof _Temporal . PlainDate ||
152+ value instanceof _Temporal . PlainMonthDay ||
153+ value instanceof _Temporal . PlainTime ||
154+ value instanceof _Temporal . PlainYearMonth
152155 ) {
153156 return true ;
154157 }
@@ -167,12 +170,7 @@ export class FluentDateTime extends FluentType<
167170 value :
168171 | number
169172 | Date
170- | Temporal . Instant
171- | Temporal . PlainDateTime
172- | Temporal . PlainDate
173- | Temporal . PlainMonthDay
174- | Temporal . PlainTime
175- | Temporal . PlainYearMonth
173+ | TemporalObject
176174 | FluentDateTime
177175 | FluentType < number > ,
178176 opts : Intl . DateTimeFormatOptions = { }
@@ -203,8 +201,15 @@ export class FluentDateTime extends FluentType<
203201 const value = this . value ;
204202 if ( typeof value === "number" ) return value ;
205203 if ( value instanceof Date ) return value . getTime ( ) ;
206- if ( 'epochMilliseconds' in value ) return value . epochMilliseconds ;
207- if ( 'toZonedDateTime' in value ) return ( value as Temporal . PlainDateTime ) . toZonedDateTime ( "UTC" ) . epochMilliseconds ;
204+
205+ if ( 'epochMilliseconds' in value ) {
206+ return value . epochMilliseconds as number ;
207+ }
208+
209+ if ( 'toZonedDateTime' in value ) {
210+ return value . toZonedDateTime ! ( "UTC" ) . epochMilliseconds ;
211+ }
212+
208213 throw new TypeError ( "Unwrapping a non-number value as a number" ) ;
209214 }
210215
0 commit comments