Skip to content
This repository was archived by the owner on Sep 3, 2022. It is now read-only.

Commit aa59e80

Browse files
author
Julio Farah
authored
[Types] Move user.js to user.ts (#172)
* [Types] Move user.js to .ts * [broken] move index.js to index.ts * split index.d.ts * stable * remove private fields from Segment Analytics * revert yarn.lock
1 parent 344b9bc commit aa59e80

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

lib/global-modules.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export {};
2+
3+
declare global {
4+
interface Window {
5+
jQuery: any;
6+
Zepto: any;
7+
}
8+
}

lib/index.d.ts renamed to lib/global.d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
declare interface Window {
2-
// TODO Remove use of any and import types for JQuery/Zepto
3-
jQuery: any;
4-
Zepto: any;
5-
}
6-
71
declare namespace SegmentAnalytics {
82
interface AnalyticsJS {}
93
}

lib/types.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
export interface SegmentAnalytics {
22
Integrations: { [name: string]: unknown };
33
options: InitOptions;
4-
_sourceMiddlewares;
5-
_integrationMiddlewares;
6-
_destinationMiddlewares;
7-
_integrations;
8-
_readied: boolean;
9-
_timeout: number;
104
}
115

126
export interface IntegrationsSettings {

lib/user.js renamed to lib/user.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* Module dependencies.
55
*/
66

7+
import { InitOptions } from './types';
78
var Entity = require('./entity');
89
var bindAll = require('bind-all');
910
var cookie = require('./cookie');
@@ -17,6 +18,22 @@ var localStorage = require('./store');
1718
* User defaults
1819
*/
1920

21+
interface UserDefaults {
22+
persist: boolean;
23+
cookie: {
24+
key: string;
25+
oldKey: string;
26+
};
27+
localStorage: {
28+
key: string;
29+
};
30+
}
31+
32+
interface User {
33+
defaults: UserDefaults;
34+
debug: unknown;
35+
}
36+
2037
User.defaults = {
2138
persist: true,
2239
cookie: {
@@ -30,11 +47,9 @@ User.defaults = {
3047

3148
/**
3249
* Initialize a new `User` with `options`.
33-
*
34-
* @param {Object} options
3550
*/
3651

37-
function User(options) {
52+
function User(options?: InitOptions) {
3853
this.defaults = User.defaults;
3954
this.debug = debug;
4055
Entity.call(this, options);
@@ -51,9 +66,6 @@ inherit(User, Entity);
5166
*
5267
* When the user id changes, the method will reset his anonymousId to a new one.
5368
*
54-
* // FIXME: What are the mixed types?
55-
* @param {string} id
56-
* @return {Mixed}
5769
* @example
5870
* // didn't change because the user didn't have previous id.
5971
* anonymousId = user.anonymousId();
@@ -74,7 +86,7 @@ inherit(User, Entity);
7486
* assert.notEqual(anonymousId, user.anonymousId());
7587
*/
7688

77-
User.prototype.id = function(id) {
89+
User.prototype.id = function(id: string): string | undefined {
7890
var prev = this._getId();
7991
var ret = Entity.prototype.id.apply(this, arguments);
8092
if (prev == null) return ret;
@@ -94,7 +106,7 @@ User.prototype.id = function(id) {
94106
* @return {String|User}
95107
*/
96108

97-
User.prototype.anonymousId = function(anonymousId) {
109+
User.prototype.anonymousId = function(anonymousId: string): string | User {
98110
var store = this.storage();
99111

100112
// set / remove
@@ -143,11 +155,9 @@ User.prototype.anonymousId = function(anonymousId) {
143155

144156
/**
145157
* Set the user's `anonymousid` in local storage.
146-
*
147-
* @param {String} id
148158
*/
149159

150-
User.prototype._setAnonymousIdInLocalStorage = function(id) {
160+
User.prototype._setAnonymousIdInLocalStorage = function(id: string) {
151161
if (!this._options.localStorageFallbackDisabled) {
152162
localStorage.set('ajs_anonymous_id', id);
153163
}
@@ -175,10 +185,9 @@ User.prototype.load = function() {
175185
* BACKWARDS COMPATIBILITY: Load the old user from the cookie.
176186
*
177187
* @api private
178-
* @return {boolean}
179188
*/
180189

181-
User.prototype._loadOldCookie = function() {
190+
User.prototype._loadOldCookie = function(): boolean {
182191
var user = cookie.get(this._options.cookie.oldKey);
183192
if (!user) return false;
184193

0 commit comments

Comments
 (0)