amplitude / amplitude/Amplitude-TypeScript

Generated ampli client fails type checking for events with only optional properties

Open
#347 1 comment 1 reaction 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
180
Forks
68
Avg merge
3d 1h
Merged PRs (30d)
29

Description

## Expected Behavior
The client generated from the `ampli` CLI should not produce any type errors for events with only optional properties with `exactOptionalPropertyTypes` on.

## Current Behavior
Events generated by `ampli` that only contain optional properties show the following TypeScript error
```
Class 'PlaceOrderButtonClicked' incorrectly implements interface 'BaseEvent'.
Types of property 'event_properties' are incompatible.
Type 'PlaceOrderButtonClickedProperties | undefined' is not assignable to type '{ [key: string]: any; }'.
Type 'undefined' is not assignable to type '{ [key: string]: any; }'.ts(2420)
```

Example generated event
```typescript
export class PlaceOrderButtonClicked implements BaseEvent {
event_type = 'Place Order Button Clicked';

constructor(
public event_properties?: PlaceOrderButtonClickedProperties,
) {
this.event_properties = event_properties;
}
}
```

## Possible Solution
The [`BaseEvent`](https://github.com/amplitude/Amplitude-TypeScript/blob/e6b19b58479cf131404047649647ce39c4835082/packages/analytics-types/src/base-event.ts#L4) class should have `event_properties` defined as `event_properties?: { [key: string]: any } | undefined;`

## Steps to Reproduce
1. Enable `exactOptionalPropertyTypes` in `tsconfig.json`.
2. Create an event with all optional properties.
3. Run `ampli pull` to generate the client
4. Run type checking with `tsc --noEmit` on the project

## Environment
- ampli Version: `@amplitude/ampli/1.31.2 linux-x64 node-v16.19.1`
- JS SDK Version: `1.9.1`
- Installation Method: yarn
- TypeScript version: `Version 4.9.3`
- tsconfig.json: https://github.com/tsconfig/bases/blob/main/bases/next-strictest.json
- Browser and Version: N/A

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.