Skip to content

Commit 15a32c6

Browse files
authored
Enable Typescript strict flag for app, logger, and util packages (#1897)
Full strict mode for app, logger, and util
1 parent 56414e8 commit 15a32c6

File tree

9 files changed

+62
-60
lines changed

9 files changed

+62
-60
lines changed

packages/app/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ if (isBrowser() && (self as any).firebase !== undefined) {
4343
const firebaseNamespace = createFirebaseNamespace();
4444
const initializeApp = firebaseNamespace.initializeApp;
4545

46-
firebaseNamespace.initializeApp = function() {
46+
// TODO: This disable can be removed and the 'ignoreRestArgs' option added to
47+
// the no-explicit-any rule when ESlint releases it.
48+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
49+
firebaseNamespace.initializeApp = function(...args: any) {
4750
// Environment check before initializing app
4851
// Do the check in initializeApp, so people have a chance to disable it by setting logLevel
4952
// in @firebase/logger
@@ -62,8 +65,7 @@ firebaseNamespace.initializeApp = function() {
6265
https://github.com/rollup/rollup-plugin-node-resolve
6366
`);
6467
}
65-
66-
return initializeApp.apply(undefined, arguments);
68+
return initializeApp.apply(undefined, args);
6769
};
6870

6971
export const firebase = firebaseNamespace;

packages/app/src/firebaseNamespaceCore.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,14 @@ export function createFirebaseNamespaceCore(
223223

224224
// Patch the FirebaseAppImpl prototype
225225
// @ts-ignore
226-
firebaseAppImpl.prototype[name] = function(...args) {
227-
const serviceFxn = this._getService.bind(this, name);
228-
return serviceFxn.apply(this, allowMultipleInstances ? args : []);
229-
};
226+
firebaseAppImpl.prototype[name] =
227+
// TODO: The eslint disable can be removed and the 'ignoreRestArgs'
228+
// option added to the no-explicit-any rule when ESlint releases it.
229+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
230+
function(...args: any) {
231+
const serviceFxn = this._getService.bind(this, name);
232+
return serviceFxn.apply(this, allowMultipleInstances ? args : []);
233+
};
230234

231235
return serviceNamespace;
232236
}

packages/app/tsconfig.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"extends": "../../config/tsconfig.base.json",
33
"compilerOptions": {
44
"outDir": "dist",
5-
"resolveJsonModule": true
5+
"resolveJsonModule": true,
6+
"strict": true
67
},
7-
"exclude": [
8-
"dist/**/*"
9-
]
10-
}
8+
"exclude": ["dist/**/*"]
9+
}

packages/logger/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../../config/tsconfig.base.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"strict": true
56
},
67
"exclude": [
78
"dist/**/*"

packages/util/src/crypt.ts

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -86,68 +86,55 @@ const byteArrayToString = function(bytes: number[]): string {
8686
};
8787

8888
// Static lookup maps, lazily populated by init_()
89-
export const base64 = {
89+
class Base64 {
9090
/**
9191
* Maps bytes to characters.
92-
* @type {Object}
93-
* @private
9492
*/
95-
byteToCharMap_: null,
93+
byteToCharMap_: { [key: number]: string } | null = null;
9694

9795
/**
9896
* Maps characters to bytes.
99-
* @type {Object}
100-
* @private
10197
*/
102-
charToByteMap_: null,
98+
charToByteMap_: { [key: string]: number } | null = null;
10399

104100
/**
105101
* Maps bytes to websafe characters.
106-
* @type {Object}
107-
* @private
108102
*/
109-
byteToCharMapWebSafe_: null,
103+
byteToCharMapWebSafe_: { [key: number]: string } | null = null;
110104

111105
/**
112106
* Maps websafe characters to bytes.
113-
* @type {Object}
114-
* @private
115107
*/
116-
charToByteMapWebSafe_: null,
108+
charToByteMapWebSafe_: { [key: string]: number } | null = null;
117109

118110
/**
119-
* Our default alphabet, shared between
111+
* Our default alphabet shared between
120112
* ENCODED_VALS and ENCODED_VALS_WEBSAFE
121-
* @type {string}
122113
*/
123-
ENCODED_VALS_BASE:
124-
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789',
114+
ENCODED_VALS_BASE: string =
115+
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789';
125116

126117
/**
127118
* Our default alphabet. Value 64 (=) is special; it means "nothing."
128-
* @type {string}
129119
*/
130-
get ENCODED_VALS() {
120+
get ENCODED_VALS(): string {
131121
return this.ENCODED_VALS_BASE + '+/=';
132-
},
122+
}
133123

134124
/**
135125
* Our websafe alphabet.
136-
* @type {string}
137126
*/
138-
get ENCODED_VALS_WEBSAFE() {
127+
get ENCODED_VALS_WEBSAFE(): string {
139128
return this.ENCODED_VALS_BASE + '-_.';
140-
},
129+
}
141130

142131
/**
143132
* Whether this browser supports the atob and btoa functions. This extension
144133
* started at Mozilla but is now implemented by many browsers. We use the
145134
* ASSUME_* variables to avoid pulling in the full useragent detection library
146135
* but still allowing the standard per-browser compilations.
147-
*
148-
* @type {boolean}
149136
*/
150-
HAS_NATIVE_SUPPORT: typeof atob === 'function',
137+
HAS_NATIVE_SUPPORT: boolean = typeof atob === 'function';
151138

152139
/**
153140
* Base64-encode an array of bytes.
@@ -165,11 +152,11 @@ export const base64 = {
165152

166153
this.init_();
167154

168-
const byteToCharMap: number[] = webSafe
169-
? this.byteToCharMapWebSafe_
170-
: this.byteToCharMap_;
155+
const byteToCharMap = webSafe
156+
? this.byteToCharMapWebSafe_!
157+
: this.byteToCharMap_!;
171158

172-
const output: number[] = [];
159+
const output = [];
173160

174161
for (let i = 0; i < input.length; i += 3) {
175162
const byte1 = input[i];
@@ -200,7 +187,7 @@ export const base64 = {
200187
}
201188

202189
return output.join('');
203-
},
190+
}
204191

205192
/**
206193
* Base64-encode a string.
@@ -217,7 +204,7 @@ export const base64 = {
217204
return btoa(input);
218205
}
219206
return this.encodeByteArray(stringToByteArray(input), webSafe);
220-
},
207+
}
221208

222209
/**
223210
* Base64-decode a string.
@@ -234,7 +221,7 @@ export const base64 = {
234221
return atob(input);
235222
}
236223
return byteArrayToString(this.decodeStringToByteArray(input, webSafe));
237-
},
224+
}
238225

239226
/**
240227
* Base64-decode a string.
@@ -255,8 +242,8 @@ export const base64 = {
255242
this.init_();
256243

257244
const charToByteMap = webSafe
258-
? this.charToByteMapWebSafe_
259-
: this.charToByteMap_;
245+
? this.charToByteMapWebSafe_!
246+
: this.charToByteMap_!;
260247

261248
const output: number[] = [];
262249

@@ -294,14 +281,14 @@ export const base64 = {
294281
}
295282

296283
return output;
297-
},
284+
}
298285

299286
/**
300287
* Lazy static initialization function. Called before
301288
* accessing any of the static map variables.
302289
* @private
303290
*/
304-
init_() {
291+
init_(): void {
305292
if (!this.byteToCharMap_) {
306293
this.byteToCharMap_ = {};
307294
this.charToByteMap_ = {};
@@ -323,7 +310,7 @@ export const base64 = {
323310
}
324311
}
325312
}
326-
};
313+
}
327314

328315
/**
329316
* URL-safe base64 encoding
@@ -350,3 +337,5 @@ export const base64Decode = function(str: string): string | null {
350337
}
351338
return null;
352339
};
340+
341+
export const base64 = new Base64();

packages/util/src/deferred.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717

1818
export class Deferred<R> {
1919
promise: Promise<R>;
20-
reject: (value?: unknown) => void;
21-
resolve: (value?: unknown) => void;
20+
reject: (value?: unknown) => void = () => {};
21+
resolve: (value?: unknown) => void = () => {};
2222
constructor() {
2323
this.promise = new Promise((resolve, reject) => {
24-
this.resolve = resolve;
25-
this.reject = reject;
24+
this.resolve = resolve as (value?: unknown) => void;
25+
this.reject = reject as (value?: unknown) => void;
2626
});
2727
}
2828

packages/util/src/obj.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export const getCount = function<V>(obj: UtilObject<V>): number {
103103

104104
export const map = function<V>(
105105
obj: UtilObject<V>,
106-
fn: (value: V, key: string | number, obj: UtilObject<V>) => unknown,
106+
fn: (value: V, key: string | number, obj: UtilObject<V>) => V,
107107
context?: unknown
108108
) {
109109
var res: UtilObject<V> = {};

packages/util/src/subscribe.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ObserverProxy<T> implements Observer<T> {
7878
// Micro-task scheduling by calling task.then().
7979
private task = Promise.resolve();
8080
private finalized = false;
81-
private finalError: Error;
81+
private finalError?: Error;
8282

8383
/**
8484
* @param executor Function which can make calls to a single Observer
@@ -126,7 +126,7 @@ class ObserverProxy<T> implements Observer<T> {
126126
* call to subscribe().
127127
*/
128128
subscribe(
129-
nextOrObserver: PartialObserver<T> | Function,
129+
nextOrObserver?: PartialObserver<T> | Function,
130130
error?: ErrorFn,
131131
complete?: CompleteFn
132132
): Unsubscribe {
@@ -141,7 +141,13 @@ class ObserverProxy<T> implements Observer<T> {
141141
}
142142

143143
// Assemble an Observer object when passed as callback functions.
144-
if (implementsAnyMethods(nextOrObserver, ['next', 'error', 'complete'])) {
144+
if (
145+
implementsAnyMethods(nextOrObserver as { [key: string]: unknown }, [
146+
'next',
147+
'error',
148+
'complete'
149+
])
150+
) {
145151
observer = nextOrObserver as Observer<T>;
146152
} else {
147153
observer = {

packages/util/tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"extends": "../../config/tsconfig.base.json",
33
"compilerOptions": {
4-
"outDir": "dist"
4+
"outDir": "dist",
5+
"strict": true
56
},
67
"exclude": [
78
"dist/**/*"

0 commit comments

Comments
 (0)