Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
Type of different internfaces

i those interfaces:

export interface EventCollectorData {
  eventType: EventCollectorType;
}

export interface ClickEventCollectorData extends EventCollectorData {
  elementData: EventCollectorElementLocator;
  eventType: EventCollectorType.CLICK;
}

export interface NetworkEventCollectorData extends EventCollectorData {
  eventType: EventCollectorType.REQUEST;
  requestData: {
    method: NetworkMethod;
    url: string;
    headers: Object;
    body: {
      json: Object | null;
      text: string | null;
      formData: any;
    };
  };
}

export type EventCollectorDataType = ClickEventCollectorData | NetworkEventCollectorData;

When I want to use the EventCollectorDataType and check if elementData or requestData is exists I'm getting this error:

Property 'elementData' does not exist on type 'EventCollectorDataType'.   Property 'elementData' does not exist on type 'NetworkEventCollectorData'.

Here is an example for the function I want to do this check:

const check = (eventCollector: EventCollectorDataType) {
  if (eventCollector.elementData) {
    
  } else if (eventCollector.requestData) {
    
  }
}

How can I implement something like that?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You need to narrow down what type you're dealing with before you access properties that only exist on one part of the union. The way you've set things up, the eventType property is perfect for this (it forms a discriminated union), so I recommend the following:

const check = (eventCollector: EventCollectorDataType) {
  if (eventCollector.eventType === EventCollectorType.CLICK) {
    // do stuff with eventCollector.elementData
  } else if (eventCollector.eventType === EventCollectorType.REQUEST) {
    // do stuff with eventCollector.requestData 
  }
}

An alternative way to narrow it down would be like this:

if ('elementData' in eventCollector) {
   
} else if ('requestData' in eventCollector) {
    
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!