File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // perf_hooks.d.ts - Custom type definitions for performance.mark() and performance.measure()
2+ import type {
3+ MarkOptions ,
4+ MeasureOptions ,
5+ Performance ,
6+ PerformanceMark ,
7+ PerformanceMarkOptions ,
8+ PerformanceMeasure ,
9+ PerformanceMeasureOptions ,
10+ } from 'node:perf_hooks' ;
11+ import type {
12+ MarkOptionsWithDevtools ,
13+ MarkerPayload ,
14+ MeasureOptionsWithDevtools ,
15+ TrackEntryPayload ,
16+ WithDevToolsPayload ,
17+ } from './lib/user-timing-extensibility-api.type' ;
18+
19+ export { } ;
20+
21+ type DetailPayloadWithDevtools = WithDevToolsPayload <
22+ TrackEntryPayload | MarkerPayload
23+ > ;
24+
25+ declare module 'node:perf_hooks' {
26+ export interface PerformanceMarkOptions {
27+ detail ?: DetailPayloadWithDevtools ;
28+ startTime ?: DOMHighResTimeStamp ;
29+ }
30+
31+ export interface PerformanceMeasureOptions {
32+ detail ?: DetailPayloadWithDevtools ;
33+ start ?: string | number ;
34+ end ?: string | number ;
35+ duration ?: number ;
36+ }
37+
38+ const performance : {
39+ mark (
40+ name : string ,
41+ options ?: {
42+ detail ?: DetailPayloadWithDevtools ;
43+ } ,
44+ ) : PerformanceMark ;
45+
46+ measure (
47+ name : string ,
48+ startOrOptions ?:
49+ | string
50+ | number
51+ | {
52+ detail ?: DetailPayloadWithDevtools ;
53+ start ?: string | number ;
54+ end ?: string | number ;
55+ duration ?: number ;
56+ } ,
57+ end ?: string | number ,
58+ ) : PerformanceMeasure ;
59+ } ;
60+ }
Original file line number Diff line number Diff line change 1+ import { type PerformanceMarkOptions , performance } from 'node:perf_hooks' ;
2+ import { describe , expectTypeOf , it } from 'vitest' ;
3+
4+ describe ( 'perf_hooks.type' , ( ) => {
5+ it ( 'PerformanceMarkOptions should be type safe' , ( ) => {
6+ expectTypeOf < {
7+ startTime : number ;
8+ detail : {
9+ devtools : {
10+ dataType : 'marker' ;
11+ color : 'error' ;
12+ } ;
13+ } ;
14+ } > ( ) . toMatchTypeOf < PerformanceMarkOptions > ( ) ;
15+
16+ expectTypeOf < {
17+ startTime : number ;
18+ detail : {
19+ devtools : {
20+ dataType : 'markerr' ;
21+ } ;
22+ } ;
23+ } > ( ) . not . toMatchTypeOf < PerformanceMarkOptions > ( ) ;
24+ } ) ;
25+
26+ it ( 'perf_hooks.mark should be type safe' , ( ) => {
27+ performance . mark ( 'name' , {
28+ detail : {
29+ devtools : {
30+ dataType : 'marker' ,
31+ color : 'error' ,
32+ } ,
33+ } ,
34+ } ) ;
35+
36+ performance . mark ( 'name' , {
37+ detail : {
38+ devtools : {
39+ /* @ts -expect-error - dataType should be marker | track */
40+ dataType : 'markerrr' ,
41+ color : 'error' ,
42+ } ,
43+ } ,
44+ } ) ;
45+ } ) ;
46+
47+ it ( 'PerformanceMeasureOptions should be type safe' , ( ) => {
48+ expectTypeOf < {
49+ start : string ;
50+ end : string ;
51+ detail : {
52+ devtools : {
53+ dataType : 'track-entry' ;
54+ track : 'test-track' ;
55+ color : 'primary' ;
56+ } ;
57+ } ;
58+ } > ( ) . toMatchTypeOf < PerformanceMeasureOptions > ( ) ;
59+ } ) ;
60+
61+ it ( 'perf_hooks.measure should be type safe' , ( ) => {
62+ performance . measure ( 'measure-name' , 'start-mark' , 'end-mark' ) ;
63+
64+ performance . measure ( 'measure-name' , {
65+ start : 'start-mark' ,
66+ end : 'end-mark' ,
67+ detail : {
68+ /* @ts -expect-error - track is required */
69+ devtools : {
70+ dataType : 'track-entry' ,
71+ color : 'primary' ,
72+ } ,
73+ } ,
74+ } ) ;
75+ } ) ;
76+ } ) ;
Original file line number Diff line number Diff line change 55 "declaration" : true ,
66 "types" : [" node" ]
77 },
8- "include" : [" src/**/*.ts " ],
8+ "include" : [" src/**/*.{ts,d.ts} " ],
99 "exclude" : [
1010 " vitest.unit.config.ts" ,
1111 " vitest.int.config.ts" ,
Original file line number Diff line number Diff line change 1313 " src/**/*.test.js" ,
1414 " src/**/*.test.jsx" ,
1515 " src/**/*.d.ts" ,
16- " ../../testing/test-setup/src/vitest.d.ts"
16+ " ../../testing/test-setup/src/vitest.d.ts" ,
17+ " src/perf_hooks.type.ts"
1718 ]
1819}
You can’t perform that action at this time.
0 commit comments