Skip to content

Commit 6880668

Browse files
authored
allow optional keys in segment event type (#574)
1 parent 88d705a commit 6880668

File tree

5 files changed

+26
-19
lines changed

5 files changed

+26
-19
lines changed

.changeset/eleven-apes-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@segment/analytics-next': patch
3+
---
4+
5+
Loosen SegmentEvent and Traits typescript interface (Fixes [#570](https://github.com/segmentio/analytics-next/issues/570)).

packages/browser/qa/__tests__/smoke.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import flat from 'flat'
22
import { difference, intersection, uniq, without } from 'lodash'
3-
import { JSONValue } from '../../src/core/events'
3+
import { JSONObject } from '../../src/core/events'
44
import { browser } from '../lib/browser'
55
import { run } from '../lib/runner'
66
import { objectSchema } from '../lib/schema'
@@ -69,14 +69,14 @@ function compareSchema(results: RemovePromise<ReturnType<typeof run>>) {
6969
'integrations'
7070
)
7171

72-
expect((req.data as Record<string, JSONValue>).integrations).toEqual(
72+
expect((req.data as JSONObject).integrations).toEqual(
7373
expect.objectContaining(
74-
(classic.data as Record<string, JSONValue>).integrations
74+
(classic.data as JSONObject).integrations
7575
)
7676
)
7777

78-
const flatNext = flat(req.data) as Record<string, JSONValue>
79-
const flatClassic = flat(classic.data) as Record<string, JSONValue>
78+
const flatNext = flat(req.data) as JSONObject
79+
const flatClassic = flat(classic.data) as JSONObject
8080

8181
intersectionKeys.forEach((key) => {
8282
const comparison = {

packages/browser/src/core/events/interfaces.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ import { ID } from '../user'
55
export type JSONPrimitive = string | number | boolean | null
66
export type JSONValue = JSONPrimitive | JSONObject | JSONArray
77
export type JSONObject = { [member: string]: JSONValue }
8-
export type JSONArray = Array<JSONValue>
8+
export type JSONArray = JSONValue[]
9+
10+
/**
11+
* A JSON object that allows undefined key/values which will be completely removed during serialization.
12+
*/
13+
export type JSONObjectLoose = {
14+
[member: string]: JSONValueLoose | undefined
15+
}
16+
17+
export type JSONValueLoose = JSONPrimitive | JSONObjectLoose | JSONValueLoose[]
918

1019
export type Callback = (ctx: Context) => Promise<unknown> | unknown
1120

@@ -100,10 +109,8 @@ interface AnalyticsContext {
100109
[key: string]: any
101110
}
102111

103-
export type Traits = { [k: string]: JSONValue }
104-
export type EventProperties = {
105-
[k: string]: JSONValue
106-
}
112+
export type Traits = JSONObjectLoose
113+
export type EventProperties = JSONObjectLoose
107114

108115
export interface SegmentEvent {
109116
messageId?: string

packages/browser/src/plugins/ajs-destination/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
Integrations,
3-
JSONObject,
4-
JSONValue,
5-
SegmentEvent,
6-
} from '@/core/events'
1+
import { Integrations, JSONObject, SegmentEvent } from '@/core/events'
72
import { Alias, Facade, Group, Identify, Page, Track } from '@segment/facade'
83
import { Analytics, InitOptions } from '../../core/analytics'
94
import { LegacySettings } from '../../browser'
@@ -65,7 +60,7 @@ async function flushQueue(
6560
export class LegacyDestination implements Plugin {
6661
name: string
6762
version: string
68-
settings: Record<string, JSONValue>
63+
settings: JSONObject
6964
options: InitOptions = {}
7065
type: Plugin['type'] = 'destination'
7166
middleware: DestinationMiddlewareFunction[] = []
@@ -83,7 +78,7 @@ export class LegacyDestination implements Plugin {
8378
constructor(
8479
name: string,
8580
version: string,
86-
settings: Record<string, JSONValue> = {},
81+
settings: JSONObject = {},
8782
options: InitOptions
8883
) {
8984
this.name = name

packages/browser/src/plugins/schema-filter/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ function disabledActionDestinations(
3131
return (settings.remotePlugins ?? []).reduce((acc, p) => {
3232
if (p.settings['subscriptions']) {
3333
if (disabledRemotePlugins.includes(p.name)) {
34-
// @ts-expect-error element implicitly has an 'any' type because p.settings is a JSONValue
34+
// @ts-expect-error element implicitly has an 'any' type because p.settings is a JSONObject
3535
p.settings['subscriptions'].forEach(
3636
// @ts-expect-error parameter 'sub' implicitly has an 'any' type
3737
(sub) => (acc[`${p.name} ${sub.partnerAction}`] = false)

0 commit comments

Comments
 (0)