全局覆盖第三方导出接口

huangapple go评论57阅读模式
英文:

Global overriding third party exported interface

问题

I'm using @react-native-firebase/messaging and calling the method messaging().onNotificationOpenedApp(remoteMessage => ....)

I want to override the property data inside remoteMessage's type so I can have TS intellisense when sending notifications.

My data type should be something like:

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

I've tryed creating a global.d.ts in my project root folder with

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

But this doesn't work. How can I override this type globally and without needing to explicity set function's property type?

Thanks =D

英文:

I'm using @react-native-firebase/messaging and calling the method messaging().onNotificationOpenedApp(remoteMessage => ....)

I want to override the property data inside remoteMessage's type so I can have TS intellisense when sending notifications.

My data type should be something like:

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

I've tryed creating a global.d.ts in my project root folder with

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

But this doesn't work. How can I override this type globally and without needing to explicity set function's property type?

Thanks =D

答案1

得分: 0

以下是代码中需要翻译的部分:

type MyData = {
  page: string,
  pageProps: string,
}

type MyData2 = {
  age: string,
  name: string,
}

type RemoteMessageWithMyData = Omit<RemoteMessage, 'data'> & { data: MyData }
type RemoteMessageWithSomeOther = Omit<RemoteMessage, 'data'> & { data: MyData2 }

function handleMydata(payload: RemoteMessageWithMyData) {
  console.log(payload.data.pageProps)
}

function handleMyOtherdata(payload: RemoteMessageWithSomeOther) {
  console.log(payload.data.age)
}

function isMyOtherDataPayload(rm: RemoteMessage): rm is RemoteMessageWithSomeOther {
  if (rm.data && 'age' in rm.data) return true
  return false
}

function isMyDataPayload(rm: RemoteMessage): rm is RemoteMessageWithMyData {
  if (rm.data && 'page' in rm.data) return true
  return false
}

function onNotificationOpenedAppMyData(rm: RemoteMessage) {
  if (isMyDataPayload(rm)) {
    handleMydata(rm)
  } else if (isMyOtherDataPayload(rm)) {
    handleMyOtherdata(rm)
  }
}

onNotificationOpenedApp((dafa: RemoteMessage) => onNotificationOpenedAppMyData(dafa))
英文:

Define you own function which will use types

type MyData = {
  page: string,
  pageProps: string,
}

type MyData2 = {
  age: string,
  name: string,
}

type RemoteMessageWithMyData = Omit&lt;RemoteMessage, &#39;data&#39;&gt; &amp; {data: MyData}
type RemoteMessageWithSomeOther = Omit&lt;RemoteMessage, &#39;data&#39;&gt; &amp; {data: MyData2}

function handleMydata(payload: RemoteMessageWithMyData) {
  console.log(payload.data.pageProps)
}

function handleMyOtherdata(payload: RemoteMessageWithSomeOther) {
  console.log(payload.data.age)
}

function isMyOtherDataPayload (rm: RemoteMessage): rm is RemoteMessageWithSomeOther  {
 if (rm.data &amp;&amp; &#39;age&#39; in rm.data) return true
 return false
}

function isMyDataPayload (rm: RemoteMessage): rm is RemoteMessageWithMyData  {
 if (rm.data &amp;&amp; &#39;page&#39; in rm.data) return true
 return false
}

function onNotificationOpenedAppMyData(rm: RemoteMessage) {
  if (isMyDataPayload(rm)) {
   handleMydata(rm)
  } else if (isMyOtherDataPayload(rm)){
    handleMyOtherdata(rm)
  }

}

onNotificationOpenedApp((dafa:RemoteMessage) =&gt; onNotificationOpenedAppMyData(dafa))

huangapple
  • 本文由 发表于 2023年3月31日 02:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定