Skip to content

Commit 849d832

Browse files
authored
Enforce either userId or anonymousId as identifiers (DefinitelyTyped#43633)
1 parent 9922d95 commit 849d832

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@
3232
"_comment": "This will remove husky from when we started migrating to use prettier",
3333
"pre-commit": "npm uninstall husky"
3434
}
35-
}
35+
},
36+
"dependencies": {}
3637
}

Diff for: types/analytics-node/analytics-node-tests.ts

+18
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,24 @@ function testTrack(): void {
5959
}
6060
});
6161

62+
analytics.track({
63+
anonymousId: '019mr8mf4r',
64+
event: 'Purchased an Item',
65+
properties: {
66+
revenue: 39.95,
67+
shippingMethod: '2-day'
68+
}
69+
});
70+
71+
// $ExpectError
72+
analytics.track({
73+
event: 'Purchased an Item',
74+
properties: {
75+
revenue: 39.95,
76+
shippingMethod: '2-day'
77+
}
78+
});
79+
6280
analytics.track({
6381
userId: '019mr8mf4r',
6482
event: 'Purchased an Item',

Diff for: types/analytics-node/index.d.ts

+12-19
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
// Definitions by: Andrew Fong <https://github.com/fongandrew>
44
// Thomas Thiebaud <https://github.com/thomasthiebaud>
55
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
6+
// TypeScript Version: 2.1
67

78
export = AnalyticsNode.Analytics;
89

910
declare namespace AnalyticsNode {
10-
interface Message {
11+
type Identity =
12+
| { userId: string | number }
13+
| { userId?: string | number; anonymousId: string | number };
14+
15+
type Message = Identity & {
1116
type: string;
1217
context: {
1318
library: {
@@ -22,9 +27,7 @@ declare namespace AnalyticsNode {
2227
};
2328
timestamp?: Date;
2429
messageId?: string;
25-
anonymousId?: string | number;
26-
userId?: string | number;
27-
}
30+
};
2831

2932
interface Data {
3033
batch: Message[];
@@ -49,19 +52,15 @@ declare namespace AnalyticsNode {
4952

5053
/* The identify method lets you tie a user to their actions and record
5154
traits about them. */
52-
identify(message: {
53-
userId?: string | number;
54-
anonymousId?: string | number;
55+
identify(message: Identity & {
5556
traits?: any;
5657
timestamp?: Date;
5758
context?: any;
5859
integrations?: Integrations;
5960
}, callback?: (err: Error, data: Data) => void): Analytics;
6061

6162
/* The track method lets you record the actions your users perform. */
62-
track(message: {
63-
userId?: string | number;
64-
anonymousId?: string | number;
63+
track(message: Identity & {
6564
event: string;
6665
properties?: any;
6766
timestamp?: Date;
@@ -71,9 +70,7 @@ declare namespace AnalyticsNode {
7170

7271
/* The page method lets you record page views on your website, along with
7372
optional extra information about the page being viewed. */
74-
page(message: {
75-
userId?: string | number;
76-
anonymousId?: string | number;
73+
page(message: Identity & {
7774
category?: string;
7875
name?: string;
7976
properties?: any;
@@ -83,18 +80,14 @@ declare namespace AnalyticsNode {
8380
}, callback?: (err: Error, data: Data) => void): Analytics;
8481

8582
/* alias is how you associate one identity with another. */
86-
alias(message: {
83+
alias(message: Identity & {
8784
previousId: string | number;
88-
userId?: string | number;
89-
anonymousId?: string | number;
9085
integrations?: Integrations;
9186
}, callback?: (err: Error, data: Data) => void): Analytics;
9287

9388
/* Group calls can be used to associate individual users with shared
9489
accounts or companies. */
95-
group(message: {
96-
userId?: string | number;
97-
anonymousId?: string | number;
90+
group(message: Identity & {
9891
groupId: string | number;
9992
traits?: any;
10093
context?: any;

0 commit comments

Comments
 (0)