Skip to content

Commit

Permalink
Fix typescript compat
Browse files Browse the repository at this point in the history
  • Loading branch information
amortemousque committed Jan 22, 2025
1 parent 222c207 commit e1cef02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
2 changes: 0 additions & 2 deletions packages/rum-core/src/domain/view/viewCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export function startViewCollection(
const { service, version, id, name, context } = viewHistory.findView(startTime)!

return {
// Explicitly set the event type to enhance type checking, especially for cases
// where conditional logic depends on the `type` field (e.g., `if (eventType === 'VIEW')`).
type: eventType,
service,
version,
Expand Down
18 changes: 15 additions & 3 deletions packages/rum-core/src/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ export const enum HookNames {
}

type RecursivePartialExcept<T, K extends keyof T> = {
[P in keyof T as P extends K ? never : P]?: T[P] extends object ? RecursivePartialExcept<T[P], never> : T[P]
[P in keyof T]?: T[P] extends object ? RecursivePartialExcept<T[P], never> : T[P]
} & {
[P in keyof T as P extends K ? P : never]-?: T[P]
[P in K]: T[P]
}

// Define a partially recursive type for RUM events, ensuring the `type` field is always present.
// This improves type checking, especially in conditional logic in hooks (e.g., `if (eventType === 'VIEW')`).
export type PartialRumEvent = RecursivePartialExcept<RumEvent, 'type'>

// This is a workaround for an issue occurring when the Browser SDK is included in a TypeScript
// project configured with `isolatedModules: true`. Even if the const enum is declared in this
// module, we cannot use it directly to define the EventMap interface keys (TS error: "Cannot access
// ambient const enums when the '--isolatedModules' flag is provided.").
declare const HookNamesAsConst: {
ASSEMBLE: HookNames.Assemble
}
export type HookCallbackMap = {
[HookNames.Assemble]: (param: { eventType: RumEvent['type']; startTime: RelativeTime }) => PartialRumEvent | undefined
[HookNamesAsConst.ASSEMBLE]: (param: {
eventType: RumEvent['type']
startTime: RelativeTime
}) => PartialRumEvent | undefined
}

export type Hooks = ReturnType<typeof startHooks>
Expand Down

0 comments on commit e1cef02

Please sign in to comment.