Skip to content

Commit 15fca6d

Browse files
committed
refactor(database): Type casting format for TSX support
1 parent 98b115b commit 15fca6d

28 files changed

+107
-109
lines changed

src/database/api/DataSnapshot.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export class DataSnapshot {
101101
validateArgCount('DataSnapshot.getPriority', 0, 0, arguments.length);
102102

103103
// typecast here because we never return deferred values or internal priorities (MAX_PRIORITY)
104-
return /**@type {string|number|null} */ <string | number | null>(this.node_.getPriority().val());
104+
return (this.node_.getPriority().val() as string | number | null);
105105
}
106106

107107
/**
@@ -119,7 +119,7 @@ export class DataSnapshot {
119119
if (this.node_.isLeafNode())
120120
return false;
121121

122-
const childrenNode = /**@type {ChildrenNode} */ <ChildrenNode>(this.node_);
122+
const childrenNode = (this.node_ as ChildrenNode);
123123
// Sanitize the return value to a boolean. ChildrenNode.forEachChild has a weird return type...
124124
return !!childrenNode.forEachChild(this.index_, (key, node) => {
125125
return action(new DataSnapshot(node, this.ref_.child(key), PRIORITY_INDEX));

src/database/api/Database.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,10 @@ export class Database implements FirebaseService {
7373
validateUrl(apiName, 1, parsedURL);
7474

7575
const repoInfo = parsedURL.repoInfo;
76-
if (repoInfo.host !== (<RepoInfo>(<any>this.repo_).repoInfo_).host) {
76+
if (repoInfo.host !== ((this.repo_ as any).repoInfo_ as RepoInfo).host) {
7777
fatal(apiName + ': Host name does not match the current database: ' +
7878
'(found ' + repoInfo.host + ' but expected ' +
79-
(<RepoInfo>(<any>this.repo_).repoInfo_).host + ')');
79+
((this.repo_ as any).repoInfo_ as RepoInfo).host + ')');
8080
}
8181

8282
return this.ref(parsedURL.path.toString());
@@ -113,11 +113,11 @@ class DatabaseInternals {
113113

114114
/** @return {Promise<void>} */
115115
delete(): Promise<void> {
116-
(<any>this.database).checkDeleted_('delete');
117-
RepoManager.getInstance().deleteRepo(/** @type {!Repo} */ <Repo>((<any>this.database).repo_));
116+
(this.database as any).checkDeleted_('delete');
117+
RepoManager.getInstance().deleteRepo((this.database as any).repo_ as Repo);
118118

119-
(<any>this.database).repo_ = null;
120-
(<any>this.database).root_ = null;
119+
(this.database as any).repo_ = null;
120+
(this.database as any).root_ = null;
121121
this.database.INTERNAL = null;
122122
this.database = null;
123123
return PromiseImpl.resolve();

src/database/api/Query.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ export class Query {
133133
// This is a slight hack. We cannot goog.require('fb.api.Firebase'), since Firebase requires fb.api.Query.
134134
// However, we will always export 'Firebase' to the global namespace, so it's guaranteed to exist by the time this
135135
// method gets called.
136-
return <Reference>(new Query.__referenceConstructor(this.repo, this.path));
136+
return (new Query.__referenceConstructor(this.repo, this.path) as Reference);
137137
}
138138

139139
/**
@@ -492,7 +492,7 @@ export class Query {
492492
context?: Object): { cancel: ((a: Error) => void) | null, context: Object | null } {
493493
const ret: { cancel: ((a: Error) => void) | null, context: Object | null } = {cancel: null, context: null};
494494
if (cancelOrContext && context) {
495-
ret.cancel = <(a: Error) => void>(cancelOrContext);
495+
ret.cancel = (cancelOrContext as (a: Error) => void);
496496
validateCallback(fnName, 3, ret.cancel, true);
497497

498498
ret.context = context;

src/database/api/Reference.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class Reference extends Query {
9292
getRoot(): Reference {
9393
validateArgCount('Reference.root', 0, 0, arguments.length);
9494

95-
let ref = <any>this;
95+
let ref = (this as any);
9696
while (ref.getParent() !== null) {
9797
ref = ref.getParent();
9898
}

src/database/core/Repo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ export class Repo {
168168
*/
169169
serverTime(): number {
170170
const offsetNode = this.infoData_.getNode(new Path('.info/serverTimeOffset'));
171-
const offset = <number>(offsetNode.val()) || 0;
171+
const offset = (offsetNode.val() as number) || 0;
172172
return new Date().getTime() + offset;
173173
}
174174

@@ -199,14 +199,14 @@ export class Repo {
199199
let events = [];
200200
if (tag) {
201201
if (isMerge) {
202-
const taggedChildren = map(<{ [k: string]: any }>data, (raw: any) => nodeFromJSON(raw));
202+
const taggedChildren = map(data as { [k: string]: any }, (raw: any) => nodeFromJSON(raw));
203203
events = this.serverSyncTree_.applyTaggedQueryMerge(path, taggedChildren, tag);
204204
} else {
205205
const taggedSnap = nodeFromJSON(data);
206206
events = this.serverSyncTree_.applyTaggedQueryOverwrite(path, taggedSnap, tag);
207207
}
208208
} else if (isMerge) {
209-
const changedChildren = map(<{ [k: string]: any }>data, (raw: any) => nodeFromJSON(raw));
209+
const changedChildren = map(data as { [k: string]: any }, (raw: any) => nodeFromJSON(raw));
210210
events = this.serverSyncTree_.applyServerMerge(path, changedChildren);
211211
} else {
212212
const snap = nodeFromJSON(data);

src/database/core/Repo_transaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ Repo.prototype.startTransaction = function (path: Path,
307307
events = events.concat(this.serverSyncTree_.ackUserWrite(queue[i].currentWriteId));
308308
if (queue[i].onComplete) {
309309
// We never unset the output snapshot, and given that this transaction is complete, it should be set
310-
const node = <Node>(queue[i].currentOutputSnapshotResolved);
310+
const node = queue[i].currentOutputSnapshotResolved as Node;
311311
const ref = new Reference(this, queue[i].path);
312312
const snapshot = new DataSnapshot(node, ref, PRIORITY_INDEX);
313313
callbacks.push(queue[i].onComplete.bind(null, null, true, snapshot));

src/database/core/SparseSnapshotTree.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class SparseSnapshotTree {
3434
const childKey = path.getFront();
3535
path = path.popFront();
3636
if (this.children_.contains(childKey)) {
37-
const childTree = <SparseSnapshotTree>this.children_.get(childKey);
37+
const childTree = this.children_.get(childKey) as SparseSnapshotTree;
3838
return childTree.find(path);
3939
} else {
4040
return null;
@@ -67,7 +67,7 @@ export class SparseSnapshotTree {
6767
this.children_.add(childKey, new SparseSnapshotTree());
6868
}
6969

70-
const child = <SparseSnapshotTree>this.children_.get(childKey);
70+
const child = this.children_.get(childKey) as SparseSnapshotTree;
7171
path = path.popFront();
7272
child.remember(path, data);
7373
}
@@ -104,7 +104,7 @@ export class SparseSnapshotTree {
104104
const childKey = path.getFront();
105105
path = path.popFront();
106106
if (this.children_.contains(childKey)) {
107-
const safeToRemove = (<SparseSnapshotTree>this.children_.get(childKey)).forget(path);
107+
const safeToRemove = (this.children_.get(childKey) as SparseSnapshotTree).forget(path);
108108
if (safeToRemove) {
109109
this.children_.remove(childKey);
110110
}

src/database/core/WriteTree.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class WriteTree {
239239
* @return {!ChildrenNode}
240240
*/
241241
calcCompleteEventChildren(treePath: Path, completeServerChildren: ChildrenNode | null) {
242-
let completeChildren = <Node>ChildrenNode.EMPTY_NODE;
242+
let completeChildren = ChildrenNode.EMPTY_NODE as Node;
243243
const topLevelSet = this.visibleWrites_.getCompleteNode(treePath);
244244
if (topLevelSet) {
245245
if (!topLevelSet.isLeafNode()) {
@@ -385,8 +385,8 @@ export class WriteTree {
385385
if (!toIterate.isEmpty() && !toIterate.isLeafNode()) {
386386
const nodes = [];
387387
const cmp = index.getCompare();
388-
const iter = reverse ? (<ChildrenNode>toIterate).getReverseIteratorFrom(startPost, index) :
389-
(<ChildrenNode>toIterate).getIteratorFrom(startPost, index);
388+
const iter = reverse ? (toIterate as ChildrenNode).getReverseIteratorFrom(startPost, index) :
389+
(toIterate as ChildrenNode).getIteratorFrom(startPost, index);
390390
let next = iter.getNext();
391391
while (next && nodes.length < count) {
392392
if (cmp(next, startPost) !== 0) {
@@ -563,7 +563,7 @@ export class WriteTreeRef {
563563
* @return {!ChildrenNode}
564564
*/
565565
calcCompleteEventChildren(completeServerChildren: ChildrenNode | null): ChildrenNode {
566-
return <ChildrenNode>this.writeTree_.calcCompleteEventChildren(this.treePath_, completeServerChildren);
566+
return this.writeTree_.calcCompleteEventChildren(this.treePath_, completeServerChildren) as ChildrenNode;
567567
}
568568

569569
/**

src/database/core/snap/ChildrenNode.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ export class ChildrenNode implements Node {
186186
// convert to array.
187187
const array: Object[] = [];
188188
for (let key in obj)
189-
array[<number><any>key] = obj[key];
189+
array[key as any as number] = obj[key];
190190

191191
return array;
192192
} else {
@@ -204,7 +204,7 @@ export class ChildrenNode implements Node {
204204
let toHash = '';
205205
if (!this.getPriority().isEmpty())
206206
toHash += 'priority:' + priorityHashText(
207-
<string | number>(this.getPriority().val())) + ':';
207+
(this.getPriority().val() as string | number)) + ':';
208208

209209
this.forEachChild(PRIORITY_INDEX, function (key, childNode) {
210210
const childHash = childNode.hash();
@@ -406,7 +406,7 @@ export class ChildrenNode implements Node {
406406
else if (other.isLeafNode()) {
407407
return false;
408408
} else {
409-
const otherChildrenNode = /** @type {!ChildrenNode} */ <ChildrenNode>(other);
409+
const otherChildrenNode = other as ChildrenNode;
410410
if (!this.getPriority().equals(otherChildrenNode.getPriority())) {
411411
return false;
412412
} else if (this.children_.count() === otherChildrenNode.children_.count()) {

src/database/core/snap/LeafNode.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,12 @@ export class LeafNode implements Node {
159159
let toHash = '';
160160
if (!this.priorityNode_.isEmpty())
161161
toHash += 'priority:' + priorityHashText(
162-
<number|string>(this.priorityNode_.val())) + ':';
162+
(this.priorityNode_.val() as number|string)) + ':';
163163

164164
const type = typeof this.value_;
165165
toHash += type + ':';
166166
if (type === 'number') {
167-
toHash += doubleToIEEE754String(<number>(this.value_));
167+
toHash += doubleToIEEE754String(this.value_ as number);
168168
} else {
169169
toHash += this.value_;
170170
}
@@ -191,7 +191,7 @@ export class LeafNode implements Node {
191191
return -1;
192192
} else {
193193
assert(other.isLeafNode(), 'Unknown node type');
194-
return this.compareToLeafNode_(<LeafNode>(other));
194+
return this.compareToLeafNode_(other as LeafNode);
195195
}
196196
}
197197

@@ -253,7 +253,7 @@ export class LeafNode implements Node {
253253
return true;
254254
}
255255
else if (other.isLeafNode()) {
256-
const otherLeaf = <LeafNode>(other);
256+
const otherLeaf = other as LeafNode;
257257
return this.value_ === otherLeaf.value_ && this.priorityNode_.equals(otherLeaf.priorityNode_);
258258
} else {
259259
return false;

src/database/core/snap/childSet.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ export const buildChildSet = function<K,V>(childList: NamedNode[],
6464
return null;
6565
} else if (length == 1) {
6666
namedNode = childList[low];
67-
key = keyFn ? keyFn(namedNode) : <K><any>namedNode;
68-
return new LLRBNode(key, <V><any>namedNode.node, LLRBNode.BLACK, null, null);
67+
key = keyFn ? keyFn(namedNode) : namedNode as any as K;
68+
return new LLRBNode(key, namedNode.node as any as V, LLRBNode.BLACK, null, null);
6969
} else {
7070
const middle = parseInt((length / 2 as any), 10) + low;
7171
const left = buildBalancedTree(low, middle);
7272
const right = buildBalancedTree(middle + 1, high);
7373
namedNode = childList[middle];
74-
key = keyFn ? keyFn(namedNode) : <K><any>namedNode;
75-
return new LLRBNode(key, <V><any>namedNode.node, LLRBNode.BLACK, left, right);
74+
key = keyFn ? keyFn(namedNode) : namedNode as any as K;
75+
return new LLRBNode(key, namedNode.node as any as V, LLRBNode.BLACK, left, right);
7676
}
7777
};
7878

@@ -87,8 +87,8 @@ export const buildChildSet = function<K,V>(childList: NamedNode[],
8787
index -= chunkSize;
8888
const childTree = buildBalancedTree(low + 1, high);
8989
const namedNode = childList[low];
90-
const key: K = keyFn ? keyFn(namedNode) : <K><any>namedNode;
91-
attachPennant(new LLRBNode(key, <V><any>namedNode.node, color, null, childTree));
90+
const key: K = keyFn ? keyFn(namedNode) : namedNode as any as K;
91+
attachPennant(new LLRBNode(key, namedNode.node as any as V, color, null, childTree));
9292
};
9393

9494
const attachPennant = function (pennant: LLRBNode<K, V>) {
@@ -119,5 +119,5 @@ export const buildChildSet = function<K,V>(childList: NamedNode[],
119119
const base12 = new Base12Num(childList.length);
120120
const root = buildFrom12Array(base12);
121121

122-
return new SortedMap<K, V>(mapSortFn || <any>cmp, root);
122+
return new SortedMap<K, V>(mapSortFn || (cmp as any), root);
123123
};

src/database/core/snap/nodeFromJSON.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function nodeFromJSON(json: any | null,
3232
priority === null ||
3333
typeof priority === 'string' ||
3434
typeof priority === 'number' ||
35-
(typeof priority === 'object' && '.sv' in <object>priority),
35+
(typeof priority === 'object' && '.sv' in (priority as object)),
3636
'Invalid priority type found: ' + (typeof priority)
3737
);
3838

@@ -42,14 +42,14 @@ export function nodeFromJSON(json: any | null,
4242

4343
// Valid leaf nodes include non-objects or server-value wrapper objects
4444
if (typeof json !== 'object' || '.sv' in json) {
45-
const jsonLeaf = <string | number | boolean | object>(json);
45+
const jsonLeaf = json as string | number | boolean | object;
4646
return new LeafNode(jsonLeaf, nodeFromJSON(priority));
4747
}
4848

4949
if (!(json instanceof Array) && USE_HINZE) {
5050
const children: NamedNode[] = [];
5151
let childrenHavePriority = false;
52-
const hinzeJsonObj: { [k: string]: any } = <object>(json);
52+
const hinzeJsonObj: { [k: string]: any } = json as object;
5353
forEach(hinzeJsonObj, (key: string, child: any) => {
5454
if (typeof key !== 'string' || key.substring(0, 1) !== '.') { // Ignore metadata nodes
5555
const childNode = nodeFromJSON(hinzeJsonObj[key]);
@@ -64,10 +64,8 @@ export function nodeFromJSON(json: any | null,
6464
return ChildrenNode.EMPTY_NODE;
6565
}
6666

67-
const childSet = /**@type {!SortedMap.<string, !Node>} */ <SortedMap<string, Node>>(buildChildSet(
68-
children, NAME_ONLY_COMPARATOR, function (namedNode) { return namedNode.name; },
69-
NAME_COMPARATOR
70-
));
67+
const childSet = buildChildSet(children, NAME_ONLY_COMPARATOR,
68+
(namedNode) => namedNode.name, NAME_COMPARATOR) as SortedMap<string, Node>;
7169
if (childrenHavePriority) {
7270
const sortedChildSet = buildChildSet(children, PRIORITY_INDEX.getCompare());
7371
return new ChildrenNode(childSet, nodeFromJSON(priority),
@@ -78,7 +76,7 @@ export function nodeFromJSON(json: any | null,
7876
}
7977
} else {
8078
let node: Node = ChildrenNode.EMPTY_NODE;
81-
const jsonObj = /** @type {!Object} */ <object>(json);
79+
const jsonObj = json as object;
8280
forEach(jsonObj, (key: string, childData: any) => {
8381
if (contains(jsonObj, key)) {
8482
if (key.substring(0, 1) !== '.') { // ignore metadata nodes.

src/database/core/util/CountedSet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class CountedSet<K, V> {
1313
* @param {V} val
1414
*/
1515
add(item: K, val: V) {
16-
this.set[<any>item] = val !== null ? val : <any>true;
16+
this.set[item as any] = val !== null ? val : (true as any);
1717
}
1818

1919
/**
@@ -29,14 +29,14 @@ export class CountedSet<K, V> {
2929
* @return {V}
3030
*/
3131
get(item: K): V | void {
32-
return this.contains(item) ? this.set[<any>item] : undefined;
32+
return this.contains(item) ? this.set[item as any] : undefined;
3333
}
3434

3535
/**
3636
* @param {!K} item
3737
*/
3838
remove(item: K) {
39-
delete this.set[<any>item];
39+
delete this.set[item as any];
4040
}
4141

4242
/**

src/database/core/util/Path.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export class Path {
2626
*/
2727
constructor(pathOrString: string | string[], pieceNum?: number) {
2828
if (pieceNum === void 0) {
29-
this.pieces_ = (<string>pathOrString).split('/');
29+
this.pieces_ = (pathOrString as string).split('/');
3030

3131
// Remove empty pieces.
3232
let copyTo = 0;
@@ -40,7 +40,7 @@ export class Path {
4040

4141
this.pieceNum_ = 0;
4242
} else {
43-
this.pieces_ = <string[]>pathOrString;
43+
this.pieces_ = pathOrString as string[];
4444
this.pieceNum_ = pieceNum;
4545
}
4646
}

src/database/core/util/ServerValues.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const generateWithValues = function (values: { [k: string]: any } | null)
2929
export const resolveDeferredValue = function (value: { [k: string]: any } | string | number | boolean,
3030
serverValues: { [k: string]: any }): string | number | boolean {
3131
if (!value || (typeof value !== 'object')) {
32-
return <string | number | boolean>(value);
32+
return value as string | number | boolean;
3333
} else {
3434
assert('.sv' in value, 'Unexpected leaf node or priority contents');
3535
return serverValues[value['.sv']];
@@ -62,20 +62,20 @@ export const resolveDeferredValueTree = function (tree: SparseSnapshotTree, serv
6262
* @return {!Node}
6363
*/
6464
export const resolveDeferredValueSnapshot = function (node: Node, serverValues: Object): Node {
65-
const rawPri = <Object | boolean | null | number | string>(node.getPriority().val());
65+
const rawPri = node.getPriority().val() as object | boolean | null | number | string;
6666
const priority = resolveDeferredValue(rawPri, serverValues);
6767
let newNode: Node;
6868

6969
if (node.isLeafNode()) {
70-
const leafNode = <LeafNode>(node);
70+
const leafNode = node as LeafNode;
7171
const value = resolveDeferredValue(leafNode.getValue(), serverValues);
7272
if (value !== leafNode.getValue() || priority !== leafNode.getPriority().val()) {
7373
return new LeafNode(value, nodeFromJSON(priority));
7474
} else {
7575
return node;
7676
}
7777
} else {
78-
const childrenNode = <ChildrenNode>(node);
78+
const childrenNode = node as ChildrenNode;
7979
newNode = childrenNode;
8080
if (priority !== childrenNode.getPriority().val()) {
8181
newNode = newNode.updatePriority(new LeafNode(priority));

0 commit comments

Comments
 (0)