Skip to content

Commit 8a96c3c

Browse files
committed
get rid of ts-expect-error, better typing
1 parent 3881650 commit 8a96c3c

3 files changed

Lines changed: 110 additions & 113 deletions

File tree

index.js

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const ARRAY_TYPES = [Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint1
44
const VERSION = 3; // serialized format version
55

66
/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor} TypedArrayConstructor */
7+
/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array} TypedArray */
78

89
export default class Flatbush {
910

@@ -18,8 +19,7 @@ export default class Flatbush {
1819
throw new Error('byteOffset must be 8-byte aligned.');
1920
}
2021

21-
// @ts-expect-error duck typing array buffers
22-
if (!data || data.byteLength === undefined || data.buffer) {
22+
if (!data || data.byteLength === undefined || 'buffer' in data) {
2323
throw new Error('Data must be an instance of ArrayBuffer or SharedArrayBuffer.');
2424
}
2525

@@ -79,12 +79,15 @@ export default class Flatbush {
7979
throw new Error(`Unexpected typed array class: ${ArrayType}.`);
8080
}
8181

82+
/** @type {new(b: ArrayBufferLike, o: number, l: number) => TypedArray} */
83+
const BoxCtor = ArrayType;
84+
/** @type {new(b: ArrayBufferLike, o: number, l: number) => Uint16Array | Uint32Array} */
85+
const IdxCtor = this.IndexArrayType;
86+
8287
if (data) {
8388
this.data = data;
84-
// @ts-expect-error TS can't handle SharedArraBuffer overloads
85-
this._boxes = new ArrayType(data, byteOffset + 8, numNodes * 4);
86-
// @ts-expect-error TS can't handle SharedArraBuffer overloads
87-
this._indices = new this.IndexArrayType(data, byteOffset + 8 + nodesByteSize, numNodes);
89+
this._boxes = new BoxCtor(data, byteOffset + 8, numNodes * 4);
90+
this._indices = new IdxCtor(data, byteOffset + 8 + nodesByteSize, numNodes);
8891

8992
this._pos = numNodes * 4;
9093
this.minX = this._boxes[this._pos - 4];
@@ -94,10 +97,8 @@ export default class Flatbush {
9497

9598
} else {
9699
const data = this.data = new ArrayBufferType(8 + nodesByteSize + numNodes * this.IndexArrayType.BYTES_PER_ELEMENT);
97-
// @ts-expect-error TS can't handle SharedArraBuffer overloads
98-
this._boxes = new ArrayType(data, 8, numNodes * 4);
99-
// @ts-expect-error TS can't handle SharedArraBuffer overloads
100-
this._indices = new this.IndexArrayType(data, 8 + nodesByteSize, numNodes);
100+
this._boxes = new BoxCtor(data, 8, numNodes * 4);
101+
this._indices = new IdxCtor(data, 8 + nodesByteSize, numNodes);
101102
this._pos = 0;
102103
this.minX = Infinity;
103104
this.minY = Infinity;
@@ -228,7 +229,7 @@ export default class Flatbush {
228229
const end = Math.min(nodeIndex + this.nodeSize * 4, upperBound(nodeIndex, this._levelBounds));
229230

230231
// search through child nodes
231-
for (let /** @type number */ pos = nodeIndex; pos < end; pos += 4) {
232+
for (let /** @type {number} */ pos = nodeIndex; pos < end; pos += 4) {
232233
// check if node bbox intersects with query bbox
233234
const x0 = this._boxes[pos];
234235
if (maxX < x0) continue;
@@ -301,18 +302,14 @@ export default class Flatbush {
301302
}
302303

303304
// pop items from the queue
304-
// @ts-expect-error q.length check eliminates undefined values
305-
while (q.length && (q.peek() & 1)) {
306-
const dist = q.peekValue();
307-
// @ts-expect-error
305+
while (q.length && (/** @type {number} */(q.peek()) & 1)) {
306+
const dist = /** @type {number} */(q.peekValue());
308307
if (dist > maxDistSquared) break outer;
309-
// @ts-expect-error
310-
results.push(q.pop() >> 1);
308+
results.push(/** @type {number} */(q.pop()) >> 1);
311309
if (results.length === maxResults) break outer;
312310
}
313311

314-
// @ts-expect-error
315-
nodeIndex = q.length ? q.pop() >> 1 : undefined;
312+
nodeIndex = q.length ? /** @type {number} */(q.pop()) >> 1 : undefined;
316313
}
317314

318315
q.clear();
@@ -342,7 +339,7 @@ function upperBound(value, arr) {
342339
/**
343340
* Custom quicksort that partially sorts bbox data alongside the hilbert values.
344341
* @param {Uint32Array} values
345-
* @param {InstanceType<TypedArrayConstructor>} boxes
342+
* @param {TypedArray} boxes
346343
* @param {Uint16Array | Uint32Array} indices
347344
* @param {number} left
348345
* @param {number} right
@@ -380,7 +377,7 @@ function sort(values, boxes, indices, left, right, nodeSize) {
380377
/**
381378
* Swap two values and two corresponding boxes.
382379
* @param {Uint32Array} values
383-
* @param {InstanceType<TypedArrayConstructor>} boxes
380+
* @param {TypedArray} boxes
384381
* @param {Uint16Array | Uint32Array} indices
385382
* @param {number} i
386383
* @param {number} j

0 commit comments

Comments
 (0)