Skip to content

React Native client only generates types for events that have properties #284

Open
@stevehanson

Description

@stevehanson

Thank you for adding React Native support for this library! I am starting on integrating Segment into my React Native project and am using analytics-react-native. I just added Typewriter v8, and it is working great, except it only generates types for events that have custom properties. Here are my observations for events that have no custom properties:

  • These events do show up in the rules array in plan.json jsonSchema.properties is {} as expected.
  • in segment.tsx, there is no mention whatsoever of these events:
    • the interface is not generated
    • the extendedClient.myCustomEvent = () => ... function is not created in extendSegmentClient

I've verified that if I go into Segment and add even a single property to the event and then rerun npx typewriter -u, or if I just manually add properties to plan.json, the events are added to segment.tsx as expected.

Proposed solution

Ideally, if I had an event called My Custom Event that takes no properties, something like the following would be generated in segment.tsx:

// generated segment.tsx

export type MyCustomEvent = undefined
// alternatively: export type MyCustomEvent = Record<string, never> | undefined

...
// this part is the same as for other events
extendedClient.myCustomEvent = (message: MyCustomEvent) => {
  client.track('My Custom Event', message as unknown as JsonMap)
}

The second argument of client.track is already typed to accept undefined, so this doesn't result in any errors with the client or TypeScript.

Here is my typewriter.yml:

client:
  language: typescript
  sdk: analytics-react-native
  languageOptions: {}
trackingPlans:
  - id: my_tracking_plan_id
    path: src/__generated__

I'd be happy to take a pass at fixing this and opening a PR but might need a bit of help getting pointed in the right direction. Thanks again for this library! 🙂

Steps to reproduce

  • Initialize typewriter using the React Native defaults (or use the shared config file above)
  • Set up a Segment event that has no custom properties
  • Run npx typewriter or npx typewriter -u
  • The new event will not be included in the generated segment.tsx

Versions

  • typewriter/8.0.10 darwin-arm64 node-v18.9.0
  • @segment/analytics-react-native@2.10.1
  • @segment/sovran-react-native@0.4.5
  • MacOS Ventura 13.0.1

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