Skip to content

Commit 98504e2

Browse files
authored
Fix eventprops trait (#579)
* fix event-properties and traits type * undefined properties Co-authored-by: Seth Silesky <silesky@users.noreply.github.com>
1 parent 8d48bdc commit 98504e2

File tree

3 files changed

+59
-21
lines changed

3 files changed

+59
-21
lines changed

.changeset/popular-beds-design.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+
Fix SegmentEvent and EventProperties and add tests

packages/browser/src/browser/__tests__/typedef-tests/analytics-browser.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,40 @@ export default {
7474
assertIs<Promise<User>>(grpResult)
7575
}
7676
},
77+
'Identify should work with spread objects ': () => {
78+
const user = {
79+
name: 'john',
80+
id: 12345,
81+
}
82+
const { id, ...traits } = user
83+
void AnalyticsBrowser.load({ writeKey: 'foo' }).identify('foo', traits)
84+
},
85+
'Track should work with spread objects': () => {
86+
const user = {
87+
name: 'john',
88+
id: 12345,
89+
}
90+
const { id, ...traits } = user
91+
void AnalyticsBrowser.load({ writeKey: 'foo' }).track('foo', traits)
92+
},
93+
'Identify should work with generic objects ': () => {
94+
const user = {
95+
name: 'john',
96+
id: 12345,
97+
}
98+
void AnalyticsBrowser.load({ writeKey: 'foo' }).identify('foo', user)
99+
},
100+
'Context should have a key allowing arbitrary properties': async () => {
101+
const [_, ctx] = await AnalyticsBrowser.load({ writeKey: 'foo' })
102+
const properties = ctx.event.properties!
103+
104+
properties.category.baz = 'hello'
105+
},
106+
'Track should allow undefined properties': () => {
107+
type User = {
108+
name?: string
109+
thing: 123
110+
}
111+
void AnalyticsBrowser.load({ writeKey: 'foo' }).track('foo', {} as User)
112+
},
77113
}

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,10 @@ interface AnalyticsContext {
100100
[key: string]: any
101101
}
102102

103-
// "object" is not ideal, but with JSONObject, we ran into at least one bad edge case around index sig.
103+
// This is not ideal, but it works with all the edge cases
104+
export type Traits = Record<string, any>
104105

105-
/**
106-
* An object literal representing traits
107-
* - identify: https://segment.com/docs/connections/spec/identify/#traits
108-
* - group: https://segment.com/docs/connections/spec/group/#traits
109-
* @example
110-
* { name: "john", age: 25 }
111-
*/
112-
export type Traits = object & JSONObject // intersection adds an index signature
113-
114-
/**
115-
* An object literal representing Segment event properties
116-
* - track: https://segment.com/docs/connections/spec/track/#properties
117-
* - page: https://segment.com/docs/connections/spec/page/#properties
118-
* - screen: https://segment.com/docs/connections/spec/screen/#properties
119-
* @example
120-
* { artistID: 2435325, songID: 13532532 }
121-
*/
122-
export type EventProperties = object & JSONObject // intersection adds an index signature
106+
export type EventProperties = Record<string, any>
123107

124108
export interface SegmentEvent {
125109
messageId?: string
@@ -129,9 +113,22 @@ export interface SegmentEvent {
129113
// page specific
130114
category?: string
131115
name?: string
132-
116+
/**
117+
* An object literal representing Segment event properties
118+
* - track: https://segment.com/docs/connections/spec/track/#properties
119+
* - page: https://segment.com/docs/connections/spec/page/#properties
120+
* - screen: https://segment.com/docs/connections/spec/screen/#properties
121+
* @example
122+
* { artistID: 2435325, songID: 13532532 }
123+
*/
133124
properties?: EventProperties
134-
125+
/**
126+
* An object literal representing traits
127+
* - identify: https://segment.com/docs/connections/spec/identify/#traits
128+
* - group: https://segment.com/docs/connections/spec/group/#traits
129+
* @example
130+
* { name: "john", age: 25 }
131+
*/
135132
traits?: Traits
136133

137134
integrations?: Integrations

0 commit comments

Comments
 (0)