Skip to content

IterableInAppMessage.createdAt type seems inconsistent with what's specified in the TS declaration - causing crashes #581

Open

Description

@alex-a-pereira

Issue

The type is declared to be a Date (potentially undefined)

// https://github.com/Iterable/react-native-sdk/blob/master/ts/IterableInAppMessage.ts
declare class IterableInAppMessage {
    /**
     * the ID for the in-app message
     */
    readonly messageId: string;
    /**
     * the campaign ID for this message
     */
    readonly campaignId: number;
    /**
     * when to trigger this in-app
     */
    readonly trigger: IterableInAppTrigger;
    /**
     * when was this message created
     */
    readonly createdAt?: Date;
    /**

However when i add the following logging:

   console.log('=====================================')
   console.log(latestInboxMessage?.itbInAppMessage?.createdAt)
   console.log(typeof latestInboxMessage.itbInAppMessage.createdAt)

I see that typeof createdAt is a string when logging from Android and a Number when logging from iOS

Android logs:
i included the error that is thrown that's causing my app to crash (android only)

LOG  1720797936904
 LOG  string
 ERROR  Error: fromMillis requires a numerical input, but received a string with value 1720797936904

iOS Logs

LOG 1720797936904
 LOG  number

Investigation (so far)

I'm not sure what is causing the typeof to be different between iOS/Android, however by reading the code it doesn't look like the typing of Date | undefined is correct. dateObject.setUTCMilliseconds returns a Number, not a Date

// https://github.com/Iterable/react-native-sdk/blob/master/ts/IterableInAppMessage.ts#L115
    let createdAt = dict["createdAt"]
    if (createdAt) {
      var dateObject = new Date(0)
      createdAt = dateObject.setUTCMilliseconds(createdAt)
    }

Activity

  1. alex-a-pereira commented on Oct 7, 2024

    @alex-a-pereira
    ContributorAuthor

    If anyone else runs into this issue this is how I fixed in my codebase temporarily:

    Using patch-package make this patch so TS will fail to compile if I don't account for the typings

    diff --git a/node_modules/@iterable/react-native-sdk/js/IterableInAppMessage.d.ts b/node_modules/@iterable/react-native-sdk/js/IterableInAppMessage.d.ts
    index b363e19..41efec4 100644
    --- a/node_modules/@iterable/react-native-sdk/js/IterableInAppMessage.d.ts
    +++ b/node_modules/@iterable/react-native-sdk/js/IterableInAppMessage.d.ts
    @@ -19,7 +19,7 @@ declare class IterableInAppMessage {
         /**
          * when was this message created
          */
    -    readonly createdAt?: Date;
    +    readonly createdAt?: string | number | Date;
         /**
          * when to expire this in-app (undefined means do not expire)
          */

    Then I made a helper function in my code

    const parseDateFromUnknownFormat = (iterableDate: string | number | Date | undefined): Date | null => {
      if (iterableDate == null) { return null }
    
      if (typeof iterableDate === 'string') {
        return new Date(parseInt(iterableDate))
      }
    
      if (typeof iterableDate === 'number') {
        return new Date(iterableDate)
      }
    
      return iterableDate
    }
  2. Ayyanchira commented on Oct 7, 2024

    @Ayyanchira
    Member

    Thanks for reporting this issue @alex-a-pereira ! and appreciate the workaround suggested for fellow developers.
    Will have this issue flagged internally for the team to fix.

  3. added a commit that references this issue on Apr 7, 2026
    b0fd3ca
  4. added a commit that references this issue on Apr 7, 2026
    d778d57
  5. added a commit that references this issue on Apr 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions