Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 incorrect type for context in beforeSend #3235

Open
thumbsupep opened this issue Dec 19, 2024 · 1 comment
Open

🐛 incorrect type for context in beforeSend #3235

thumbsupep opened this issue Dec 19, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@thumbsupep
Copy link

Describe the bug
With the addition of handlingStack on the context in beforeSend, I am getting a typescript error trying to access it.

To Reproduce
Steps to reproduce the behavior:

  1. npm install latest @datadog/browser-rum
  2. Implement beforeSend in a typescript file
  3. attempt to access context.handlingStack
  4. See typescript error
Property 'handlingStack' does not exist on type 'RumActionEventDomainContext | RumFetchResourceEventDomainContext | RumXhrResourceEventDomainContext | RumOtherResourceEventDomainContext | RumErrorEventDomainContext | RumLongTaskEventDomainContext | RumVitalEventDomainContext'.

Expected behavior
No typescript error occurs when trying to access handlingStack

I understand that handlingStack does not exist on some rum event types, but for the ones where it does exist, I should be able to access that property without typescript errors.

I have tried a workaround to create a custom type that I can cast to when accessing handlingStack like so

type RumEventContextWithHandlingStack = Exclude<
  RumEventDomainContext,
  RumViewEventDomainContext | RumOtherResourceEventDomainContext
>;

but there are other types I would need to exclude that are only internal types.

@thumbsupep thumbsupep added the bug Something isn't working label Dec 19, 2024
@BenoitZugmeyer
Copy link
Member

Thank you for your feedback. Before attempting to access handlingStack, you should do if ('handlingStack' in context). This is a TypeScript limitation, but maybe we could improve the developer experience by making the type less strict ... Instead of having:

type RumEventDomainContext = { handlingStack: string, ... } | { performanceEntry: PerformanceEntry, ... }

we could have optional properties:

type RumEventDomainContext = { handlingStack?: string, performanceEntry?: PerformanceEntry, ...}

I'll chat with the team, but in the meantime, please use if ('handlingStack' in context)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants