1
- import ESGetIntrinsic from 'es-abstract/GetIntrinsic.js ' ;
1
+ import { Temporal } from '. ' ;
2
2
3
3
import { DEBUG } from './debug' ;
4
- const INTRINSICS = { } ;
5
4
6
- const customUtilInspectFormatters = {
5
+ type TemporalIntrinsics = Omit < typeof Temporal , 'Now' > ;
6
+ type TemporalIntrinsicRegistrations = {
7
+ [ key in keyof TemporalIntrinsics as `Temporal.${key } `] : TemporalIntrinsics [ key ] ;
8
+ } ;
9
+ type TemporalIntrinsicPrototypeRegistrations = {
10
+ [ key in keyof TemporalIntrinsics as `Temporal.${key } .prototype`] : TemporalIntrinsics [ key ] [ 'prototype' ] ;
11
+ } ;
12
+ type TemporalIntrinsicRegisteredKeys = {
13
+ [ key in keyof TemporalIntrinsicRegistrations as `%${key } %`] : TemporalIntrinsicRegistrations [ key ] ;
14
+ } ;
15
+
16
+ interface StandaloneIntrinsics {
17
+ 'Temporal.Calendar.from' : typeof Temporal . Calendar . from ;
18
+ 'Temporal.TimeZone.prototype.getOffsetNanosecondsFor' : typeof Temporal . TimeZone . prototype . getOffsetNanosecondsFor ;
19
+ }
20
+ type RegisteredStandaloneIntrinsics = { [ key in keyof StandaloneIntrinsics as `%${key } %`] : StandaloneIntrinsics [ key ] } ;
21
+ const INTRINSICS : Partial < TemporalIntrinsicRegisteredKeys & RegisteredStandaloneIntrinsics > = { } ;
22
+
23
+ type customFormatFunction < T > = (
24
+ this : T ,
25
+ depth : number ,
26
+ options : { stylize : ( value : unknown , type : 'number' | 'special' ) => string }
27
+ ) => string ;
28
+ const customUtilInspectFormatters : Partial <
29
+ { [ key in keyof TemporalIntrinsicRegistrations ] : customFormatFunction < TemporalIntrinsicRegistrations [ key ] > }
30
+ > = {
7
31
[ 'Temporal.Duration' ] ( depth , options ) {
8
32
const descr = options . stylize ( `${ this [ Symbol . toStringTag ] } <${ this } >` , 'special' ) ;
9
33
if ( depth < 1 ) return descr ;
@@ -30,7 +54,10 @@ function defaultUtilInspectFormatter(this: any, depth, options) {
30
54
return options . stylize ( `${ this [ Symbol . toStringTag ] } <${ this } >` , 'special' ) ;
31
55
}
32
56
33
- export function MakeIntrinsicClass ( Class , name ) {
57
+ export function MakeIntrinsicClass (
58
+ Class : TemporalIntrinsicRegistrations [ typeof name ] ,
59
+ name : keyof TemporalIntrinsicRegistrations
60
+ ) {
34
61
Object . defineProperty ( Class . prototype , Symbol . toStringTag , {
35
62
value : name ,
36
63
writable : false ,
@@ -62,12 +89,25 @@ export function MakeIntrinsicClass(Class, name) {
62
89
DefineIntrinsic ( `${ name } .prototype` , Class . prototype ) ;
63
90
}
64
91
65
- export function DefineIntrinsic ( name , value ) {
92
+ type IntrinsicDefinitionKeys =
93
+ | keyof TemporalIntrinsicRegistrations
94
+ | keyof TemporalIntrinsicPrototypeRegistrations
95
+ | keyof StandaloneIntrinsics ;
96
+ export function DefineIntrinsic < KeyT extends keyof TemporalIntrinsicRegistrations > (
97
+ name : KeyT ,
98
+ value : TemporalIntrinsicRegistrations [ KeyT ]
99
+ ) ;
100
+ export function DefineIntrinsic < KeyT extends keyof TemporalIntrinsicPrototypeRegistrations > (
101
+ name : KeyT ,
102
+ value : TemporalIntrinsicPrototypeRegistrations [ KeyT ]
103
+ ) ;
104
+ export function DefineIntrinsic < KeyT extends keyof StandaloneIntrinsics > ( name : KeyT , value : StandaloneIntrinsics [ KeyT ] ) ;
105
+ export function DefineIntrinsic < KeyT > ( name : KeyT , value : never ) ;
106
+ export function DefineIntrinsic < KeyT extends IntrinsicDefinitionKeys > ( name : KeyT , value : unknown ) {
66
107
const key = `%${ name } %` ;
67
108
if ( INTRINSICS [ key ] !== undefined ) throw new Error ( `intrinsic ${ name } already exists` ) ;
68
109
INTRINSICS [ key ] = value ;
69
110
}
70
-
71
- export function GetIntrinsic ( intrinsic ) {
72
- return intrinsic in INTRINSICS ? INTRINSICS [ intrinsic ] : ESGetIntrinsic ( intrinsic ) ;
111
+ export function GetIntrinsic < KeyT extends keyof typeof INTRINSICS > ( intrinsic : KeyT ) : typeof INTRINSICS [ KeyT ] {
112
+ return INTRINSICS [ intrinsic ] ;
73
113
}
0 commit comments