International replacement of external interface exportation

Currently, I am utilizing the @react-native-firebase/messaging library and invoking the method

messaging().onNotificationOpenedApp(remoteMessage => ....)

I am aiming to replace the property data within the type of remoteMessage in order to benefit from TypeScript intellisense when sending notifications.

The updated data type that I am looking to achieve is:

data: {
  page: PageType
  pageProps: {
    id: string
    ....
  }
}

I have attempted to define a global.d.ts file in the root directory of my project with the following:

declare global {
  namespace FirebaseMessagingTypes {
    interface RemoteMessage extends MyCustomDataType { }
  }
}

Unfortunately, this approach did not yield the desired outcome. Is there a way to globally override this type without explicitly setting the function's property type?

Thank you =D

Answer №1

Create a custom function that utilizes data types

type MyInfo = {
  page: string,
  pageDetails: string,
}

type UserData = {
  age: string,
  fullName: string,
}

type MessageWithMyInfo = Omit<Message, 'data'> & {data: MyInfo}
type MessageWithUserData = Omit<Message, 'data'> & {data: UserData}

function processMyInfo(payload: MessageWithMyInfo) {
  console.log(payload.data.pageDetails)
}

function processUserData(payload: MessageWithUserData) {
  console.log(payload.data.age)
}

function isUserPayload (msg: Message): msg is MessageWithUserData  {
 if (msg.data && 'age' in msg.data) return true
 return false
}

function isMyInfoPayload (msg: Message): msg is MessageWithMyInfo  {
 if (msg.data && 'page' in msg.data) return true
 return false
}

function handleNotificationOpenEvent(rm: Message) {
  if (isMyInfoPayload(rm)) {
   processMyInfo(rm)
  } else if (isUserPayload(rm)){
    processUserData(rm)
  }

}

onNotificationOpenedApp((data: Message) => handleNotificationOpenEvent(data))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering an issue: a function is required to return a value if its declared type is not 'undefined', 'void', or 'any'

I have a specific function type that is capable of returning either void or Promise<void: export type CommandHandler = (values: CommandValues) => void | Promise<void>; Currently, I am attempting to utilize this function type in a void function ...

Struggling to set up a Jest testing module for a NestJs service. Encountering an issue where Nest is unable to resolve dependencies of the UsersService, specifically the Config

Greetings, fellow developers! I am excited to ask my first set of questions on stackoverflow :) Currently, I am working on a test/learning application to enhance my skills in NestJS and Vue. During the implementation of server-side unit tests using Jest, ...

The Angular Animation constantly resets with each new action taken

In my Angular project, I am working on a scaling animation for a list. I want the animation to only trigger when specific buttons (red and green) are pressed. Currently, the animation restarts regardless of what I click on. Can anyone help me troubleshoot ...

The attribute 'map' is not found on the object of type 'List'

I am currently working on a project that involves lists, each with its own title. However, while using Map, I encountered the following error: Property 'map' does not exist on type 'List' Any suggestions on how to resolve this issue? ...

Obtaining undefined values for req and resolvedUrl in GetServerSideProps function

In my project, I am currently using next.js version ""next": "^12.1.4"" and node version ""@types/node": "^14.14.6". I have created a function called getServerSideProps with parameters req and resolvedUrl. When the ...

I am experiencing difficulties with bringing in node modules into my project

Trying to incorporate @mui/styles into my React project resulted in encountering some errors as shown below. npm ERR! Could not resolve dependency: npm ERR! peer react@"^17.0.0" from @mui/<a href="/cdn-cgi/l/email-protection" class="__cf_email ...

The global variable in TypeScript is not specified; it is initialized within the declaration `declare global{}`

In my TypeScript code, I'm facing a challenge when trying to add a global scope variable. In JavaScript (NodeJS), this process is straightforward: // index.js globalThis.helloWorld = 'Hello World!'; require('./log.js') // log.js c ...

Use the rowTemplate in a Kendo grid without replacing the existing one

I am currently utilizing Angular 1.4 with TypeScript and Kendo UI (employing angular directives). My goal is to create a RowTemplate for each row that dynamically changes the color based on a specific property of the item. While I am aware of jQuery solu ...

In Angular 5, a variable's value becomes undefined once it is subscribed to outside of its assigned

I keep encountering an undefined value when trying to assign the subscribed value to a variable in my code snippet below. service.ts getIpAddress() : Observable<any> { return this.http .get(this.Geo_Api) .map((response: ...

Using ReactJS with Typescript: attempting to interpret a value as a type error is encountered (TS2749)

Currently, I am working on coding a ReactJS class using Typescript and Material-ui in a .tsx file. In one of the custom components that I have created, I need to establish a reference to another component used within this custom component. export class My ...

Running the `npm start` command in Angular tends to be quite time-consuming

When I use Visual Studio Code to run Angular projects, my laptop seems to take a longer time when running the server through npm start compared to others. Could this delay be related to my PC specifications, or is there something I can do to improve it? ...

The module declaration is triggering a lint error that reads "Unexpected token, expecting '{'"

To address the absence of available types for a library, I created the file omnivore.d.ts in TypeScript. Within this file, I added the following line: declare module '@mapbox/leaflet-omnivore' Upon running vue-cli-service lint in my project, an ...

Implementing unique behaviors based on data types in Typescript

I'm currently working on a React project where I need to showcase different types of articles, which I refer to as "Previews." These articles can be either text-based or contain images/videos. To handle this, I've defined two interfaces (TextPre ...

Put Jest to the test by testing the appendFileSync function

I am currently working on creating a test for appendfilesync function. When using a logger, I noticed that one line of code is not covered in my tests. Below is the code snippet I am referring to (please note that I am using tslog for logging purposes): ex ...

Testing the NestJS service with a real database comparison

I'm looking to test my Nest service using a real database, rather than just a mock object. While I understand that most unit tests should use mocks, there are times when testing against the actual database is more appropriate. After scouring through ...

Display an API generated popup list using Vue's rendering capabilities

I'm attempting to generate a pop-up within a displayed list using custom content retrieved from an API request. Currently, my code looks like this: <template> <div class="biblio__all"> <a v-for="i in items" ...

Retrieve the structure from a React application

When it comes to documenting architecture, the process can be incredibly beneficial but also quite time-consuming and prone to becoming outdated quickly. I have come across tools like Doxygen that are able to extract architectural details such as dependen ...

Deactivate the button while the form is being submitted

I need a way to prevent users from clicking the submit button multiple times while the form is being processed by the server. Below is the solution I have come up with: clear() { this.count++ this.formGroup.get('name').reset(null); ...

selectize.js typescript: Unable to access values of an undefined object (reading '0')

I've been working on incorporating selectize.js into my project using webpack and typescript. After installing selectize.js and the necessary types, I added the following to my code: yarn add @selectize/selectize yarn add @types/select2 Within my c ...

The element 'loginToken' is not found within the type '{ loginToken: string; } | { error: Error; } | { username: string; password: string; }'

I'm currently working on creating a reducer using Typescript and Redux, but I keep running into this error: Property 'loginToken' is not recognized in type '{ loginToken: string; } | { error: Error; } | { username: string; password: str ...