Skip to content

Commit 3ced340

Browse files
committed
refactor: move type parameter from measureCtx outer to inner function
- Move generic type parameter <T> from measureCtx function to the returned function - Update MeasureOptions parameter to use MeasureOptions<T> for proper typing - Remove type casts in profiler measure and measureAsync methods - Fix measureAsync to properly await work function before calling success This change allows the type system to properly infer the result type T throughout the measurement chain without requiring type assertions.
1 parent 3572fef commit 3ced340

2 files changed

Lines changed: 9 additions & 12 deletions

File tree

packages/utils/src/lib/profiler/profiler.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ export class Profiler<T extends Record<string, ActionTrackEntryPayload>> {
175175
return work();
176176
}
177177

178-
const { start, success, error } = this.ctxOf(
179-
event,
180-
options as MeasureOptions,
181-
);
178+
const { start, success, error } = this.ctxOf(event, options);
182179
start();
183180
try {
184181
const r = work();
@@ -215,10 +212,7 @@ export class Profiler<T extends Record<string, ActionTrackEntryPayload>> {
215212
return await work();
216213
}
217214

218-
const { start, success, error } = this.ctxOf(
219-
event,
220-
options as MeasureOptions,
221-
);
215+
const { start, success, error } = this.ctxOf(event, options);
222216
start();
223217
try {
224218
const r = await work();

packages/utils/src/lib/user-timing-extensibility-api-utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ export function mergeDevtoolsPayload<
330330
{} as MergeResult<P> & { properties?: DevToolsProperties },
331331
);
332332
}
333-
333+
export type ActionTrackConfigs<T extends string = string> = Record<
334+
T,
335+
ActionTrackEntryPayload
336+
>;
334337
/**
335338
* Sets up tracks with default values merged into each track.
336339
* This helps to avoid repetition when defining multiple tracks with common properties.
@@ -347,7 +350,7 @@ export function setupTracks<
347350
key,
348351
mergeDevtoolsPayload(defaults, track),
349352
]),
350-
);
353+
) satisfies ActionTrackConfigs;
351354
}
352355

353356
/**
@@ -468,10 +471,10 @@ export type MeasureCtxOptions = ActionTrackEntryPayload & {
468471
* - `error(error)`: Completes failed measurement with error metadata
469472
*/
470473

471-
export function measureCtx<T = unknown>(cfg: MeasureCtxOptions) {
474+
export function measureCtx(cfg: MeasureCtxOptions) {
472475
const { prefix, error: globalErr, ...defaults } = cfg;
473476

474-
return (event: string, opt?: MeasureOptions) => {
477+
return <T = unknown>(event: string, opt?: MeasureOptions<T>) => {
475478
const { success, error, ...measurePayload } = opt ?? {};
476479
const merged = mergeDevtoolsPayload(defaults, measurePayload);
477480
const {

0 commit comments

Comments
 (0)