Skip to content

Commit fcc9606

Browse files
committed
[types] improve types
1 parent 8c13b84 commit fcc9606

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

index.d.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
type TypedArray =
2-
| Int8Array
3-
| Uint8Array
4-
| Uint8ClampedArray
5-
| Int16Array
6-
| Uint16Array
7-
| Int32Array
8-
| Uint32Array
9-
| Float32Array
10-
| Float64Array
11-
| BigInt64Array
12-
| BigUint64Array;
1+
import type { TypedArray } from 'is-typed-array';
2+
3+
declare namespace typedArrayByteLength {
4+
export { TypedArray };
5+
}
136

147
declare function typedArrayByteLength(value: TypedArray): number;
158
declare function typedArrayByteLength(value: unknown): false;

index.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ var isTypedArray = require('is-typed-array');
88

99
var typedArrays = require('available-typed-arrays')();
1010

11-
/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
12-
/** @typedef {import('possible-typed-array-names')[number]} TypedArrayNames */
13-
/** @typedef {(value: TypedArray) => number} Getter */
11+
/** @typedef {import('possible-typed-array-names')[number]} TypedArrayName */
12+
/** @typedef {(value: import('.').TypedArray) => number} Getter */
1413

15-
/** @type {Object.<TypedArrayNames, Getter>} */
16-
var getters = {};
14+
/** @type {Partial<Record<TypedArrayName, Getter> & { __proto__: null }>} */
15+
var getters = {
16+
__proto__: null
17+
};
1718

1819
var oDP = Object.defineProperty;
1920
if (gOPD) {
@@ -24,7 +25,9 @@ if (gOPD) {
2425
forEach(typedArrays, function (typedArray) {
2526
// In Safari 7, Typed Array constructors are typeof object
2627
if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
27-
var Proto = global[typedArray].prototype;
28+
var TA = global[typedArray];
29+
/** @type {import('.').TypedArray} */
30+
var Proto = TA.prototype;
2831
// @ts-expect-error TS doesn't narrow properly inside callbacks
2932
var descriptor = gOPD(Proto, 'byteLength');
3033
if (!descriptor && hasProto) {
@@ -55,16 +58,20 @@ if (gOPD) {
5558
/** @type {Getter} */
5659
var tryTypedArrays = function tryAllTypedArrays(value) {
5760
/** @type {number} */ var foundByteLength;
58-
forEach(getters, /** @type {(getter: Getter) => void} */ function (getter) {
59-
if (typeof foundByteLength !== 'number') {
60-
try {
61-
var byteLength = getter(value);
62-
if (typeof byteLength === 'number') {
63-
foundByteLength = byteLength;
64-
}
65-
} catch (e) {}
61+
forEach(
62+
// eslint-disable-next-line no-extra-parens
63+
/** @type {Record<TypedArrayName, Getter>} */ (getters),
64+
/** @type {(getter: Getter) => void} */ function (getter) {
65+
if (typeof foundByteLength !== 'number') {
66+
try {
67+
var byteLength = getter(value);
68+
if (typeof byteLength === 'number') {
69+
foundByteLength = byteLength;
70+
}
71+
} catch (e) {}
72+
}
6673
}
67-
});
74+
);
6875
// @ts-expect-error TS can't guarantee the callback is invoked sync
6976
return foundByteLength;
7077
};

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ test('Typed Arrays', { skip: availableTypedArrays.length === 0 }, function (t) {
6666
var buffer = new ArrayBuffer(length);
6767
var TypedArray = global[typedArray];
6868
if (isCallable(TypedArray)) {
69+
// @ts-expect-error TS doesn't seem to know about the second TA arg
6970
var arr = new TypedArray(buffer, byteLength);
7071
t.equal(typedArrayByteLength(arr), byteLength, 'new ' + typedArray + '(new ArrayBuffer(' + length + '), ' + byteLength + ') is typed array of byte Length ' + byteLength);
7172
} else {

0 commit comments

Comments
 (0)