英文:
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<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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论