Skip to content

Turn on Typescript strict option for Firestore #1941

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/firestore/src/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
private tokenListener: ((token: string | null) => void) | null = null;

/** Tracks the current User. */
private currentUser: User;
private currentUser?: User;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be initialized to User.UNAUTHENTICATED instead.


/**
* Counter used to detect if the token changed while a getToken request was
Expand Down Expand Up @@ -192,7 +192,7 @@ export class FirebaseCredentialsProvider implements CredentialsProvider {
typeof tokenData.accessToken === 'string',
'Invalid tokenData returned from getToken():' + tokenData
);
return new OAuthToken(tokenData.accessToken, this.currentUser);
return new OAuthToken(tokenData.accessToken, this.currentUser!);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ! after above suggestion.

} else {
return null;
}
Expand Down
14 changes: 7 additions & 7 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,12 +283,12 @@ class FirestoreSettings {
}

class FirestoreConfig {
databaseId: DatabaseId;
persistenceKey: string;
credentials: CredentialsProvider;
firebaseApp: FirebaseApp;
settings: FirestoreSettings;
persistence: boolean;
databaseId!: DatabaseId;
persistenceKey!: string;
credentials!: CredentialsProvider;
firebaseApp?: FirebaseApp;
settings!: FirestoreSettings;
persistence?: boolean;
}

/**
Expand All @@ -313,7 +313,7 @@ export class Firestore implements firestore.FirebaseFirestore, FirebaseService {
// TODO(mikelehen): Use modularized initialization instead.
readonly _queue = new AsyncQueue();

_dataConverter: UserDataConverter;
_dataConverter!: UserDataConverter;

constructor(databaseIdOrApp: FirestoreDatabase | FirebaseApp) {
const config = new FirestoreConfig();
Expand Down
8 changes: 4 additions & 4 deletions packages/firestore/src/core/event_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import { DocumentViewChange, ChangeType, ViewSnapshot } from './view_snapshot';
* tracked by EventManager.
*/
class QueryListenersInfo {
viewSnap: ViewSnapshot | null;
targetId: TargetId;
viewSnap: ViewSnapshot | null = null;
targetId?: TargetId;
listeners: QueryListener[] = [];
}

Expand Down Expand Up @@ -81,7 +81,7 @@ export class EventManager implements SyncEngineListener {
return targetId;
});
} else {
return Promise.resolve(queryInfo.targetId);
return Promise.resolve(queryInfo.targetId!);
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ export class QueryListener {

private options: ListenOptions;

private snap: ViewSnapshot;
private snap?: ViewSnapshot;

private onlineState: OnlineState = OnlineState.Unknown;

Expand Down
17 changes: 8 additions & 9 deletions packages/firestore/src/core/firestore_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,20 @@ export class FirestoreClient {
// NOTE: These should technically have '|undefined' in the types, since
// they're initialized asynchronously rather than in the constructor, but
// given that all work is done on the async queue and we assert that
// initialization completes before any other work is queued, we're cheating
// with the types rather than littering the code with '!' or unnecessary
// undefined checks.
private eventMgr: EventManager;
private persistence: Persistence;
private localStore: LocalStore;
private remoteStore: RemoteStore;
private syncEngine: SyncEngine;
// initialization completes before any other work is queued, we use !
// to guarantee these will be initialized when called.
private eventMgr!: EventManager;
private persistence!: Persistence;
private localStore!: LocalStore;
private remoteStore!: RemoteStore;
private syncEngine!: SyncEngine;
private lruScheduler?: LruScheduler;

private readonly clientId = AutoId.newId();
private _clientShutdown = false;

// PORTING NOTE: SharedClientState is only used for multi-tab web.
private sharedClientState: SharedClientState;
private sharedClientState!: SharedClientState;

constructor(
private platform: Platform,
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/core/sync_engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class LimboResolution {
* decide whether it needs to manufacture a delete event for the target once
* the target is CURRENT.
*/
receivedDocument: boolean;
receivedDocument: boolean = false;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/src/core/target_id_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ enum GeneratorIds {
// TODO(mrschmidt): Explore removing this class in favor of generating these IDs
// directly in SyncEngine and LocalStore.
export class TargetIdGenerator {
private nextId: TargetId;
// This is assigned in this.seek(), which is called in the constructor.
private nextId!: TargetId;

/**
* Instantiates a new TargetIdGenerator. If a seed is provided, the generator
Expand Down
12 changes: 6 additions & 6 deletions packages/firestore/src/local/indexeddb_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,23 @@ export class IndexedDbPersistence implements Persistence {
private readonly document: Document | null;
private readonly window: Window;

private simpleDb: SimpleDb;
private simpleDb!: SimpleDb;
private _started = false;
private isPrimary = false;
private networkEnabled = true;
private dbName: string;

/** Our window.unload handler, if registered. */
private windowUnloadHandler: (() => void) | null;
private windowUnloadHandler: (() => void) | null = null;
private inForeground = false;

private serializer: LocalSerializer;

/** Our 'visibilitychange' listener if registered. */
private documentVisibilityHandler: ((e?: Event) => void) | null;
private documentVisibilityHandler: ((e?: Event) => void) | null = null;

/** The client metadata refresh task. */
private clientMetadataRefresher: CancelablePromise<void>;
private clientMetadataRefresher?: CancelablePromise<void>;

/** The last time we garbage collected the Remote Document Changelog. */
private lastGarbageCollectionTime = Number.NEGATIVE_INFINITY;
Expand All @@ -267,7 +267,7 @@ export class IndexedDbPersistence implements Persistence {
private readonly indexManager: IndexedDbIndexManager;
private readonly remoteDocumentCache: IndexedDbRemoteDocumentCache;
private readonly webStorage: Storage;
private listenSequence: ListenSequence;
private listenSequence!: ListenSequence;
readonly referenceDelegate: IndexedDbLruDelegate;

// Note that `multiClientParams` must be present to enable multi-client support while multi-tab
Expand Down Expand Up @@ -1131,7 +1131,7 @@ function clientMetadataStore(

/** Provides LRU functionality for IndexedDB persistence. */
export class IndexedDbLruDelegate implements ReferenceDelegate, LruDelegate {
private inMemoryPins: ReferenceSet | null;
private inMemoryPins: ReferenceSet | null = null;

readonly garbageCollector: LruGarbageCollector;

Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/lru_garbage_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ const REGULAR_GC_DELAY_MS = 5 * 60 * 1000;
* whether or not GC is enabled, as well as which delay to use before the next run.
*/
export class LruScheduler {
private hasRun: boolean;
private hasRun: boolean = false;
private gcTask: CancelablePromise<void> | null;

constructor(
Expand Down
7 changes: 4 additions & 3 deletions packages/firestore/src/local/memory_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,9 @@ export class MemoryTransaction implements PersistenceTransaction {
}

export class MemoryEagerDelegate implements ReferenceDelegate {
private inMemoryPins: ReferenceSet | null;
private orphanedDocuments: Set<DocumentKey>;
private inMemoryPins: ReferenceSet | null = null;
// Will be set by onTransactionStarted().
private orphanedDocuments!: Set<DocumentKey>;

constructor(private readonly persistence: MemoryPersistence) {}

Expand Down Expand Up @@ -308,7 +309,7 @@ export class MemoryEagerDelegate implements ReferenceDelegate {
}

export class MemoryLruDelegate implements ReferenceDelegate, LruDelegate {
private inMemoryPins: ReferenceSet | null;
private inMemoryPins: ReferenceSet | null = null;
private orphanedSequenceNumbers: ObjectMap<
DocumentKey,
ListenSequenceNumber
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/src/local/persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import { ClientId } from './shared_client_state';
* on persistence.
*/
export abstract class PersistenceTransaction {
readonly currentSequenceNumber: ListenSequenceNumber;
// Classes that extend/implement this class always assign this in the constructor.
readonly currentSequenceNumber!: ListenSequenceNumber;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/local/persistence_promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class PersistencePromise<T> {
): PersistencePromise<void> {
const promises: Array<PersistencePromise<void>> = [];
collection.forEach((r, s) => {
promises.push(f.call(this, r, s));
promises.push(f.call(this, r, s as S));
});
return this.waitFor(promises);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/model/field_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export type FieldType = null | boolean | number | string | {};
* A field value represents a datatype as stored by Firestore.
*/
export abstract class FieldValue {
readonly typeOrder: TypeOrder;
readonly typeOrder?: TypeOrder;

abstract value(options?: FieldValueOptions): FieldType;
abstract isEqual(other: FieldValue): boolean;
Expand Down
7 changes: 4 additions & 3 deletions packages/firestore/src/model/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,10 @@ export class Precondition {
* to some source document.
*/
export abstract class Mutation {
readonly type: MutationType;
readonly key: DocumentKey;
readonly precondition: Precondition;
readonly type?: MutationType;
// All classes extending this class assign key in the constructor.
readonly key!: DocumentKey;
readonly precondition?: Precondition;

/**
* Applies this mutation to the given MaybeDocument or null for the purposes
Expand Down
7 changes: 4 additions & 3 deletions packages/firestore/src/model/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ export const DOCUMENT_KEY_NAME = '__name__';
* Path represents an ordered sequence of string segments.
*/
abstract class Path {
private segments: string[];
private offset: number;
private len: number;
// These are definitely set in init().
private segments!: string[];
private offset!: number;
private len!: number;

constructor(segments: string[], offset?: number, length?: number) {
this.init(segments, offset, length);
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/src/remote/backoff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const LOG_TAG = 'ExponentialBackoff';
* delays causing spikes of load to the backend.
*/
export class ExponentialBackoff {
private currentBaseMs: number;
private currentBaseMs: number = 0;
private timerPromise: CancelablePromise<void> | null = null;
/** The last backoff attempt, as epoch milliseconds. */
private lastAttemptTime = Date.now();
Expand Down
4 changes: 3 additions & 1 deletion packages/firestore/src/remote/persistent_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,10 @@ export class PersistentWriteStream extends PersistentStream<
*
* PersistentWriteStream manages propagating this value from responses to the
* next request.
*
* This is never null checked but it is zero-length-checked.
*/
lastStreamToken: ProtoByteString;
lastStreamToken: ProtoByteString = '';

/**
* Tracks whether or not a handshake has been successfully exchanged and
Expand Down
3 changes: 2 additions & 1 deletion packages/firestore/src/remote/remote_store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ export class RemoteStore implements TargetMetadataProvider {
}

/** SyncEngine to notify of watch and write events. */
syncEngine: RemoteSyncer;
// Will be initialized during FirestoreClient initialization.
syncEngine!: RemoteSyncer;

/**
* Starts up the remote store, creating streams, restoring state from
Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/src/remote/stream_bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import { Stream } from './connection';
* interface. The stream callbacks are invoked with the callOn... methods.
*/
export class StreamBridge<I, O> implements Stream<I, O> {
private wrappedOnOpen: () => void | undefined;
private wrappedOnClose: (err?: FirestoreError) => void | undefined;
private wrappedOnMessage: (msg: O) => void | undefined;
private wrappedOnOpen?: () => void | undefined;
private wrappedOnClose?: (err?: FirestoreError) => void | undefined;
private wrappedOnMessage?: (msg: O) => void | undefined;

private sendFn: (msg: I) => void;
private closeFn: () => void;
Expand Down
4 changes: 2 additions & 2 deletions packages/firestore/src/util/async_queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export class AsyncQueue {
private delayedOperations: Array<DelayedOperation<unknown>> = [];

// visible for testing
failure: Error;
failure?: Error;

// Flag set while there's an outstanding AsyncQueue operation, used for
// assertion sanity-checks.
Expand Down Expand Up @@ -279,7 +279,7 @@ export class AsyncQueue {
);
this.delayedOperations.push(delayedOp);

return delayedOp;
return delayedOp as DelayedOperation<T>;
}

private verifyNotFailed(): void {
Expand Down
6 changes: 4 additions & 2 deletions packages/firestore/src/util/promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ export interface CancelablePromise<T> extends Promise<T> {

export class Deferred<R> {
promise: Promise<R>;
resolve: Resolver<R>;
reject: Rejecter;
// These are assigned in the constructor, but Typescript does not recognize
// it because it is async.
resolve!: Resolver<R>;
reject!: Rejecter;

constructor() {
this.promise = new Promise((resolve: Resolver<R>, reject: Rejecter) => {
Expand Down
14 changes: 9 additions & 5 deletions packages/firestore/src/util/sorted_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,11 +526,15 @@ export class LLRBNode<K, V> {

// Represents an empty node (a leaf node in the Red-Black Tree).
export class LLRBEmptyNode<K, V> {
key: K;
value: V;
color: boolean;
left: LLRBNode<K, V>;
right: LLRBNode<K, V>;
// These values actually can be and are undefined but the isEmpty() check
// in each SortedMap function will bail out before they are called.
// Typing them as possibly null or undefined causes many errors because
// we do not null check every property.
key!: K;
value!: V;
color!: boolean;
left!: LLRBNode<K, V>;
right!: LLRBNode<K, V>;
size = 0;

// Returns a copy of the current node.
Expand Down
2 changes: 1 addition & 1 deletion packages/firestore/test/integration/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ import '../../index';
const testsContext = (require as any).context('.', true, /.test$/);
const browserTests = testsContext
.keys()
.filter(file => file.indexOf('/node/') < 0);
.filter((file: string) => file.indexOf('/node/') < 0);
browserTests.forEach(testsContext);
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describeFn('WebChannel', () => {
const makeUrl = conn.makeUrl.bind(conn);

it('includes project ID and database ID', () => {
const url = makeUrl('Commit', {});
const url = makeUrl('Commit');
expect(url).to.equal(
'http://example.com/v1/projects/testproject/' +
'databases/(default)/documents:commit'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class EventsAccumulator<
T extends firestore.DocumentSnapshot | firestore.QuerySnapshot
> {
private events: T[] = [];
private waitingFor: number;
private waitingFor: number = 0;
private deferred: Deferred<T[]> | null = null;
private rejectAdditionalEvents = false;

Expand Down
2 changes: 2 additions & 0 deletions packages/firestore/test/integration/util/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export function isRunningAgainstEmulator(): boolean {
* persistence both disabled and enabled (if the browser is supported).
*/
export const apiDescribe = apiDescribeInternal.bind(null, describe);
// @ts-ignore Patching a prototypal function.
apiDescribe.skip = apiDescribeInternal.bind(null, describe.skip);
// @ts-ignore Patching a prototypal function.
apiDescribe.only = apiDescribeInternal.bind(null, describe.only);

function apiDescribeInternal(
Expand Down
Loading