Skip to content

Commit 8b56016

Browse files
committed
v1.7.0: Previous commit did not compile
Amazingly enough, adding an `asSet` property is a breaking change, because (for example) previously `BTree<number, string>` was implicitly convertible to `BTree<number, string|number>`, but the new property somehow causes TypeScript to disallow the conversion while complaining that "Types of property 'diffAgainst' are incompatible." Yeah... sometimes I think TypeScript has a lot of bugs.
1 parent 0e53cd2 commit 8b56016

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

b+tree.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ISortedMap, ISortedMapF } from './interfaces';
1+
import { ISortedMap, ISortedMapF, ISortedSet } from './interfaces';
22
export { ISetSource, ISetSink, ISet, ISetF, ISortedSetSource, ISortedSet, ISortedSetF, IMapSource, IMapSink, IMap, IMapF, ISortedMapSource, ISortedMap, ISortedMapF } from './interfaces';
33
export declare type EditRangeResult<V, R = number> = {
44
value?: V;
@@ -456,5 +456,11 @@ export default class BTree<K = any, V = any> implements ISortedMapF<K, V>, ISort
456456
* does check that maxKey() of the children of internal nodes are sorted. */
457457
checkValid(): void;
458458
}
459+
/** A TypeScript helper function that simply returns its argument, typed as
460+
* `ISortedSet<K>` if the BTree implements it, as it does if `V extends undefined`.
461+
* If `V` cannot be `undefined`, it returns `unknown` instead. Or at least, that
462+
* was the intention, but TypeScript is acting weird and may return `ISortedSet<K>`
463+
* even if `V` can't be `undefined` (discussion: btree-typescript issue #14) */
464+
export declare function asSet<K, V>(btree: BTree<K, V>): undefined extends V ? ISortedSet<K> : unknown;
459465
/** A BTree frozen in the empty state. */
460466
export declare const EmptyBTree: BTree<any, any>;

b+tree.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ var __extends = (this && this.__extends) || (function () {
1515
};
1616
})();
1717
Object.defineProperty(exports, "__esModule", { value: true });
18-
exports.EmptyBTree = exports.simpleComparator = exports.defaultComparator = void 0;
18+
exports.EmptyBTree = exports.asSet = exports.simpleComparator = exports.defaultComparator = void 0;
1919
/**
2020
* Compares DefaultComparables to form a strict partial ordering.
2121
*
@@ -1074,11 +1074,20 @@ var BTree = /** @class */ (function () {
10741074
return BTree;
10751075
}());
10761076
exports.default = BTree;
1077+
/** A TypeScript helper function that simply returns its argument, typed as
1078+
* `ISortedSet<K>` if the BTree implements it, as it does if `V extends undefined`.
1079+
* If `V` cannot be `undefined`, it returns `unknown` instead. Or at least, that
1080+
* was the intention, but TypeScript is acting weird and may return `ISortedSet<K>`
1081+
* even if `V` can't be `undefined` (discussion: btree-typescript issue #14) */
1082+
function asSet(btree) {
1083+
return btree;
1084+
}
1085+
exports.asSet = asSet;
10771086
if (Symbol && Symbol.iterator) // iterator is equivalent to entries()
10781087
BTree.prototype[Symbol.iterator] = BTree.prototype.entries;
10791088
BTree.prototype.where = BTree.prototype.filter;
10801089
BTree.prototype.setRange = BTree.prototype.setPairs;
1081-
BTree.prototype.add = BTree.prototype.set;
1090+
BTree.prototype.add = BTree.prototype.set; // for compatibility with ISetSink<K>
10821091
function iterator(next) {
10831092
if (next === void 0) { next = (function () { return ({ done: true, value: undefined }); }); }
10841093
var result = { next: next };

b+tree.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,15 +1175,6 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
11751175
return this.hasOwnProperty('editRange');
11761176
}
11771177

1178-
/** A TypeScript helper function that returns `this as ISortedSet<K>` if this
1179-
* BTree implements it, which it does if `V extends undefined`. If `V` cannot
1180-
* be `undefined`, it returns `unknown` instead. Or at least, that was the
1181-
* intention, but TypeScript is acting weird and may return `ISortedSet<K>`
1182-
* even if `V` can't be `undefined` (discussion: btree-typescript issue #14) */
1183-
get asSet(): undefined extends V ? ISortedSet<K> : unknown {
1184-
return this as any;
1185-
}
1186-
11871178
/** Scans the tree for signs of serious bugs (e.g. this.size doesn't match
11881179
* number of elements, internal nodes not caching max element properly...)
11891180
* Computational complexity: O(number of nodes), i.e. O(size). This method
@@ -1195,20 +1186,29 @@ export default class BTree<K=any, V=any> implements ISortedMapF<K,V>, ISortedMap
11951186
}
11961187
}
11971188

1189+
/** A TypeScript helper function that simply returns its argument, typed as
1190+
* `ISortedSet<K>` if the BTree implements it, as it does if `V extends undefined`.
1191+
* If `V` cannot be `undefined`, it returns `unknown` instead. Or at least, that
1192+
* was the intention, but TypeScript is acting weird and may return `ISortedSet<K>`
1193+
* even if `V` can't be `undefined` (discussion: btree-typescript issue #14) */
1194+
export function asSet<K,V>(btree: BTree<K,V>): undefined extends V ? ISortedSet<K> : unknown {
1195+
return btree as any;
1196+
}
1197+
11981198
declare const Symbol: any;
11991199
if (Symbol && Symbol.iterator) // iterator is equivalent to entries()
12001200
(BTree as any).prototype[Symbol.iterator] = BTree.prototype.entries;
12011201
(BTree as any).prototype.where = BTree.prototype.filter;
12021202
(BTree as any).prototype.setRange = BTree.prototype.setPairs;
1203-
(BTree as any).prototype.add = BTree.prototype.set;
1203+
(BTree as any).prototype.add = BTree.prototype.set; // for compatibility with ISetSink<K>
12041204

12051205
function iterator<T>(next: () => IteratorResult<T> = (() => ({ done:true, value:undefined }))): IterableIterator<T> {
12061206
var result: any = { next };
12071207
if (Symbol && Symbol.iterator)
12081208
result[Symbol.iterator] = function() { return this; };
12091209
return result;
12101210
}
1211-
1211+
12121212

12131213
/** Leaf node / base class. **************************************************/
12141214
class BNode<K,V> {

0 commit comments

Comments
 (0)