amplitude / amplitude/Amplitude-TypeScript

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

未关闭
#347 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
TypeScript
星标
180
派生
68
平均合并
3 天 1 小时
30 天内合并 PR
29

描述

## 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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。