Skip to content

Commit ca8d829

Browse files
authored
Enable Typescript strict flag for Storage (#1935)
Enable Typescript strict flag for Storage
1 parent fc5a87c commit ca8d829

File tree

14 files changed

+79
-53
lines changed

14 files changed

+79
-53
lines changed

packages/storage/src/implementation/backoff.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,13 @@ export function start(
4949
}
5050
let triggeredCallback = false;
5151

52-
function triggerCallback(): void {
52+
// TODO: This disable can be removed and the 'ignoreRestArgs' option added to
53+
// the no-explicit-any rule when ESlint releases it.
54+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
55+
function triggerCallback(...args: any[]): void {
5356
if (!triggeredCallback) {
5457
triggeredCallback = true;
55-
callback.apply(null, arguments);
58+
callback.apply(null, args);
5659
}
5760
}
5861

@@ -63,17 +66,20 @@ export function start(
6366
}, millis);
6467
}
6568

66-
function handler(success: boolean): void {
69+
// TODO: This disable can be removed and the 'ignoreRestArgs' option added to
70+
// the no-explicit-any rule when ESlint releases it.
71+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
72+
function handler(success: boolean, ...args: any[]): void {
6773
if (triggeredCallback) {
6874
return;
6975
}
7076
if (success) {
71-
triggerCallback.apply(null, arguments);
77+
triggerCallback.call(null, success, ...args);
7278
return;
7379
}
7480
const mustStop = canceled() || hitTimeout;
7581
if (mustStop) {
76-
triggerCallback.apply(null, arguments);
82+
triggerCallback.call(null, success, ...args);
7783
return;
7884
}
7985
if (waitSeconds < 64) {

packages/storage/src/implementation/blob.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import * as type from './type';
3030
* modified after this blob's construction.
3131
*/
3232
export class FbsBlob {
33-
private data_: Blob | Uint8Array;
33+
private data_!: Blob | Uint8Array;
3434
private size_: number;
3535
private type_: string;
3636

packages/storage/src/implementation/metadata.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,20 @@ export function noXform_<T>(metadata: Metadata, value: T): T {
3838
class Mapping<T> {
3939
local: string;
4040
writable: boolean;
41-
xform: (p1: Metadata, p2: T | undefined) => T | undefined;
41+
xform: (p1: Metadata, p2?: T) => T | undefined;
4242

4343
constructor(
4444
public server: string,
4545
local?: string | null,
4646
writable?: boolean,
47-
xform?: ((p1: Metadata, p2: T | undefined) => T | undefined) | null
47+
xform?: ((p1: Metadata, p2?: T) => T | undefined) | null
4848
) {
4949
this.local = local || server;
5050
this.writable = !!writable;
5151
this.xform = xform || noXform_;
5252
}
5353
}
54-
type Mappings = Array<Mapping<unknown>>;
54+
type Mappings = Array<Mapping<string> | Mapping<number>>;
5555

5656
export { Mappings };
5757

@@ -69,15 +69,15 @@ export function getMappings(): Mappings {
6969
if (mappings_) {
7070
return mappings_;
7171
}
72-
const mappings: Array<Mapping<unknown>> = [];
73-
mappings.push(new Mapping('bucket'));
74-
mappings.push(new Mapping('generation'));
75-
mappings.push(new Mapping('metageneration'));
76-
mappings.push(new Mapping('name', 'fullPath', true));
72+
const mappings: Mappings = [];
73+
mappings.push(new Mapping<string>('bucket'));
74+
mappings.push(new Mapping<string>('generation'));
75+
mappings.push(new Mapping<string>('metageneration'));
76+
mappings.push(new Mapping<string>('name', 'fullPath', true));
7777

7878
function mappingsXformPath(
7979
_metadata: Metadata,
80-
fullPath: string
80+
fullPath: string | undefined
8181
): string | undefined {
8282
return xformPath(fullPath);
8383
}
@@ -98,18 +98,18 @@ export function getMappings(): Mappings {
9898
return size;
9999
}
100100
}
101-
const sizeMapping = new Mapping('size');
101+
const sizeMapping = new Mapping<number>('size');
102102
sizeMapping.xform = xformSize;
103103
mappings.push(sizeMapping);
104-
mappings.push(new Mapping('timeCreated'));
105-
mappings.push(new Mapping('updated'));
106-
mappings.push(new Mapping('md5Hash', null, true));
107-
mappings.push(new Mapping('cacheControl', null, true));
108-
mappings.push(new Mapping('contentDisposition', null, true));
109-
mappings.push(new Mapping('contentEncoding', null, true));
110-
mappings.push(new Mapping('contentLanguage', null, true));
111-
mappings.push(new Mapping('contentType', null, true));
112-
mappings.push(new Mapping('metadata', 'customMetadata', true));
104+
mappings.push(new Mapping<number>('timeCreated'));
105+
mappings.push(new Mapping<string>('updated'));
106+
mappings.push(new Mapping<string>('md5Hash', null, true));
107+
mappings.push(new Mapping<string>('cacheControl', null, true));
108+
mappings.push(new Mapping<string>('contentDisposition', null, true));
109+
mappings.push(new Mapping<string>('contentEncoding', null, true));
110+
mappings.push(new Mapping<string>('contentLanguage', null, true));
111+
mappings.push(new Mapping<string>('contentType', null, true));
112+
mappings.push(new Mapping<string>('metadata', 'customMetadata', true));
113113
mappings_ = mappings;
114114
return mappings_;
115115
}
@@ -134,7 +134,10 @@ export function fromResource(
134134
const len = mappings.length;
135135
for (let i = 0; i < len; i++) {
136136
const mapping = mappings[i];
137-
metadata[mapping.local] = mapping.xform(metadata, resource[mapping.server]);
137+
metadata[mapping.local] = (mapping as Mapping<unknown>).xform(
138+
metadata,
139+
resource[mapping.server]
140+
);
138141
}
139142
addRef(metadata, authWrapper);
140143
return metadata;

packages/storage/src/implementation/string.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const StringFormat = {
2727
DATA_URL: 'data_url'
2828
};
2929

30-
export function formatValidator(stringFormat: string): void {
30+
export function formatValidator(stringFormat: unknown): void {
3131
switch (stringFormat) {
3232
case StringFormat.RAW:
3333
case StringFormat.BASE64:

packages/storage/src/implementation/xhrio.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ export interface XhrIo {
4545

4646
getResponseHeader(header: string): string | null;
4747

48-
addUploadProgressListener(listener: (p1: Event) => void): void;
48+
addUploadProgressListener(listener: (p1: ProgressEvent) => void): void;
4949

50-
removeUploadProgressListener(listener: (p1: Event) => void): void;
50+
removeUploadProgressListener(listener: (p1: ProgressEvent) => void): void;
5151
}
5252

5353
/**

packages/storage/src/implementation/xhrio_network.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class NetworkXhrIo implements XhrIo {
131131
/**
132132
* @override
133133
*/
134-
addUploadProgressListener(listener: (p1: Event) => void): void {
134+
addUploadProgressListener(listener: (p1: ProgressEvent) => void): void {
135135
if (type.isDef(this.xhr_.upload)) {
136136
this.xhr_.upload.addEventListener('progress', listener);
137137
}
@@ -140,7 +140,7 @@ export class NetworkXhrIo implements XhrIo {
140140
/**
141141
* @override
142142
*/
143-
removeUploadProgressListener(listener: (p1: Event) => void): void {
143+
removeUploadProgressListener(listener: (p1: ProgressEvent) => void): void {
144144
if (type.isDef(this.xhr_.upload)) {
145145
this.xhr_.upload.removeEventListener('progress', listener);
146146
}

packages/storage/src/service.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ export class Service {
6363
* bucket.
6464
*/
6565
ref(path?: string): Reference {
66-
function validator(path: string): void {
67-
if (/^[A-Za-z]+:\/\//.test(path)) {
66+
function validator(path: unknown): void {
67+
if (typeof path !== 'string') {
68+
throw 'Path is not a string.';
69+
}
70+
if (/^[A-Za-z]+:\/\//.test(path as string)) {
6871
throw 'Expected child path but got a URL, use refFromURL instead.';
6972
}
7073
}
@@ -86,12 +89,15 @@ export class Service {
8689
* which must be a gs:// or http[s]:// URL.
8790
*/
8891
refFromURL(url: string): Reference {
89-
function validator(p: string): void {
90-
if (!/^[A-Za-z]+:\/\//.test(p)) {
92+
function validator(p: unknown): void {
93+
if (typeof p !== 'string') {
94+
throw 'Path is not a string.';
95+
}
96+
if (!/^[A-Za-z]+:\/\//.test(p as string)) {
9197
throw 'Expected full URL but got a child path, use ref instead.';
9298
}
9399
try {
94-
Location.makeFromUrl(p);
100+
Location.makeFromUrl(p as string);
95101
} catch (e) {
96102
throw 'Expected valid full URL but got an invalid one.';
97103
}

packages/storage/src/task.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ export class UploadTask {
507507
specs: ArgSpec[] | null
508508
): Subscribe<UploadTaskSnapshot> {
509509
function binder(
510-
nextOrObserver:
510+
nextOrObserver?:
511511
| NextFn<UploadTaskSnapshot>
512512
| StorageObserver<UploadTaskSnapshot>
513513
| null,
@@ -526,7 +526,7 @@ export class UploadTask {
526526
return binder;
527527
}
528528

529-
function binderNextOrObserverValidator(p: {}): void {
529+
function binderNextOrObserverValidator(p: unknown): void {
530530
if (p === null) {
531531
throw nextOrObserverMessage;
532532
}
@@ -557,7 +557,7 @@ export class UploadTask {
557557
*/
558558
then<U>(
559559
onFulfilled?: ((value: UploadTaskSnapshot) => U | Promise<U>) | null,
560-
onRejected?: ((error: unknown) => U | Promise<U>) | null
560+
onRejected?: ((error: Error) => U | Promise<U>) | null
561561
): Promise<U> {
562562
// These casts are needed so that TypeScript can infer the types of the
563563
// resulting Promise.

packages/storage/test/reference.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,8 @@ describe('Firebase Storage > Reference', () => {
591591
});
592592
it('updateMetadata throws', () => {
593593
testShared.assertThrows(
594-
root.updateMetadata.bind(root, {}),
594+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
595+
(root as any).updateMetadata.bind(root, {}),
595596
'storage/invalid-root-operation'
596597
);
597598
});

packages/storage/test/request.test.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('Firebase Storage > Request', () => {
3838
const response = 'I am the server response!!!!';
3939

4040
function newSend(xhrio: TestingXhrIo): void {
41-
const responseHeaders = {};
41+
const responseHeaders: { [key: string]: string } = {};
4242
responseHeaders[responseHeader] = responseValue;
4343
xhrio.simulateResponse(status, response, responseHeaders);
4444
}
@@ -68,7 +68,7 @@ describe('Firebase Storage > Request', () => {
6868
const args: unknown[] = spiedSend.getCall(0).args;
6969
assert.equal(args[1], url);
7070
assert.equal(args[2], method);
71-
const expectedHeaders: Headers = {};
71+
const expectedHeaders: { [key: string]: string } = {};
7272
expectedHeaders[requestHeader] = requestValue;
7373
expectedHeaders[versionHeaderName] = versionHeaderValue;
7474
assert.deepEqual(args[4], expectedHeaders);
@@ -197,7 +197,9 @@ describe('Firebase Storage > Request', () => {
197197
() => {
198198
assert.isTrue(spiedSend.calledOnce);
199199
const args: unknown[] = spiedSend.getCall(0).args;
200-
const expectedHeaders = { Authorization: 'Firebase ' + authToken };
200+
const expectedHeaders: { [key: string]: string } = {
201+
Authorization: 'Firebase ' + authToken
202+
};
201203
expectedHeaders[versionHeaderName] = versionHeaderValue;
202204
assert.deepEqual(args[4], expectedHeaders);
203205
},

packages/storage/test/service.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,11 @@ describe('Firebase Storage > Service', () => {
307307
ref.put(new Blob(['a'])).on(
308308
TaskEvent.STATE_CHANGED,
309309
null,
310-
(err: FirebaseStorageError) => {
311-
assert.equal(err.code, 'storage/app-deleted');
310+
(err: FirebaseStorageError | Error) => {
311+
assert.equal(
312+
(err as FirebaseStorageError).code,
313+
'storage/app-deleted'
314+
);
312315
resolve();
313316
},
314317
() => {

packages/storage/test/task.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ interface Response {
4545
type RequestHandler = (
4646
url: string,
4747
method: string,
48-
body?: ArrayBufferView | Blob | string,
48+
body?: ArrayBufferView | Blob | string | null,
4949
headers?: Headers
5050
) => Response;
5151

@@ -54,7 +54,7 @@ function authWrapperWithHandler(handler: RequestHandler): AuthWrapper {
5454
xhrio: TestingXhrIo,
5555
url: string,
5656
method: string,
57-
body?: ArrayBufferView | Blob | string,
57+
body?: ArrayBufferView | Blob | string | null,
5858
headers?: Headers
5959
): void {
6060
const response = handler(url, method, body, headers);
@@ -97,7 +97,7 @@ function fakeServerHandler(): RequestHandler {
9797
function handler(
9898
url: string,
9999
method: string,
100-
content?: ArrayBufferView | Blob | string,
100+
content?: ArrayBufferView | Blob | string | null,
101101
headers?: Headers
102102
): Response {
103103
method = method || 'GET';
@@ -268,7 +268,7 @@ describe('Firebase Storage > Upload Task', () => {
268268

269269
// h3: This one will get executed immediately
270270
(() => {
271-
let lastState;
271+
let lastState: TaskState;
272272
return task.on(
273273
TaskEvent.STATE_CHANGED,
274274
snapshot => {
@@ -346,7 +346,7 @@ describe('Firebase Storage > Upload Task', () => {
346346
function promiseAssertWrapper<T>(func: T): T {
347347
function wrapped(..._args: any[]): void {
348348
try {
349-
((func as any) as (...args: any[]) => void).apply(null, arguments);
349+
((func as any) as (...args: any[]) => void).apply(null, _args);
350350
} catch (e) {
351351
reject(e);
352352
// also throw to further unwind the stack

packages/storage/test/xhrio.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export interface StringHeaders {
3838
export class TestingXhrIo implements XhrIo {
3939
private state: State;
4040
private sendPromise: Promise<XhrIo>;
41-
private resolve: (xhrIo: XhrIo) => void;
41+
private resolve!: (xhrIo: XhrIo) => void;
4242
private sendHook: SendHook | null;
4343
private status: number;
4444
private responseText: string;
@@ -75,7 +75,11 @@ export class TestingXhrIo implements XhrIo {
7575
return this.sendPromise;
7676
}
7777

78-
simulateResponse(status: number, body: string, headers: Headers): void {
78+
simulateResponse(
79+
status: number,
80+
body: string,
81+
headers: { [key: string]: string }
82+
): void {
7983
if (this.state !== State.SENT) {
8084
throw new Error("Can't simulate response before send/more than once");
8185
}

packages/storage/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)