Skip to content

Commit f42bec3

Browse files
committed
[types] use a namespace; improve type
1 parent d114ee8 commit f42bec3

File tree

3 files changed

+69
-29
lines changed

3 files changed

+69
-29
lines changed

index.d.ts

+52-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
1-
type TypedArrayName =
2-
| 'Int8Array'
3-
| 'Uint8Array'
4-
| 'Uint8ClampedArray'
5-
| 'Int16Array'
6-
| 'Uint16Array'
7-
| 'Int32Array'
8-
| 'Uint32Array'
9-
| 'Float32Array'
10-
| 'Float64Array'
11-
| 'BigInt64Array'
12-
| 'BigUint64Array';
1+
declare function whichTypedArray(value: Int8Array): 'Int8Array';
2+
declare function whichTypedArray(value: Uint8Array): 'Uint8Array';
3+
declare function whichTypedArray(value: Uint8ClampedArray): 'Uint8ClampedArray';
4+
declare function whichTypedArray(value: Int16Array): 'Int16Array';
5+
declare function whichTypedArray(value: Uint16Array): 'Uint16Array';
6+
declare function whichTypedArray(value: Int32Array): 'Int32Array';
7+
declare function whichTypedArray(value: Uint32Array): 'Uint32Array';
8+
declare function whichTypedArray(value: Float32Array): 'Float32Array';
9+
declare function whichTypedArray(value: Float64Array): 'Float64Array';
10+
declare function whichTypedArray(value: BigInt64Array): 'BigInt64Array';
11+
declare function whichTypedArray(value: BigUint64Array): 'BigUint64Array';
12+
declare function whichTypedArray(value: unknown): false | null;
1313

14-
declare function whichTypedArray(value: unknown): TypedArrayName | false | null;
14+
declare namespace whichTypedArray {
15+
type TypedArrayName =
16+
| 'Int8Array'
17+
| 'Uint8Array'
18+
| 'Uint8ClampedArray'
19+
| 'Int16Array'
20+
| 'Uint16Array'
21+
| 'Int32Array'
22+
| 'Uint32Array'
23+
| 'Float32Array'
24+
| 'Float64Array'
25+
| 'BigInt64Array'
26+
| 'BigUint64Array';
27+
28+
type TypedArray =
29+
| Int8Array
30+
| Uint8Array
31+
| Uint8ClampedArray
32+
| Int16Array
33+
| Uint16Array
34+
| Int32Array
35+
| Uint32Array
36+
| Float32Array
37+
| Float64Array
38+
| BigInt64Array
39+
| BigUint64Array;
40+
41+
type TypedArrayConstructor =
42+
| Int8ArrayConstructor
43+
| Uint8ArrayConstructor
44+
| Uint8ClampedArrayConstructor
45+
| Int16ArrayConstructor
46+
| Uint16ArrayConstructor
47+
| Int32ArrayConstructor
48+
| Uint32ArrayConstructor
49+
| Float32ArrayConstructor
50+
| Float64ArrayConstructor
51+
| BigInt64ArrayConstructor
52+
| BigUint64ArrayConstructor;
53+
}
1554

1655
export = whichTypedArray;

index.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var callBind = require('call-bind');
66
var callBound = require('call-bind/callBound');
77
var gOPD = require('gopd');
88

9+
/** @type {(O: object) => string} */
910
var $toString = callBound('Object.prototype.toString');
1011
var hasToStringTag = require('has-tostringtag/shams')();
1112

@@ -15,7 +16,8 @@ var typedArrays = availableTypedArrays();
1516
var $slice = callBound('String.prototype.slice');
1617
var getPrototypeOf = Object.getPrototypeOf; // require('getprototypeof');
1718

18-
var $indexOf = callBound('Array.prototype.indexOf', true) || /** @type {(array: readonly unknown[], value: unknown) => keyof array} */ function indexOf(array, value) {
19+
/** @type {<T = unknown>(array: readonly T[], value: unknown) => number} */
20+
var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(array, value) {
1921
for (var i = 0; i < array.length; i += 1) {
2022
if (array[i] === value) {
2123
return i;
@@ -24,9 +26,8 @@ var $indexOf = callBound('Array.prototype.indexOf', true) || /** @type {(array:
2426
return -1;
2527
};
2628

27-
/** @typedef {Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array} TypedArray */
28-
/** @typedef {'Int8Array' | 'Uint8Array' | 'Uint8ClampedArray' | 'Int16Array' | 'Uint16Array' | 'Int32Array' | 'Uint32Array' | 'Float32Array' | 'Float64Array' | 'BigInt64Array' | 'BigUint64Array'} TypedArrayName */
29-
/** @type {{ [k in `\$${TypedArrayName}`]?: (receiver: TypedArray) => string | typeof Uint8Array.prototype.slice.call | typeof Uint8Array.prototype.set.call } & { __proto__: null }} */
29+
/** @typedef {(receiver: import('.').TypedArray) => string | typeof Uint8Array.prototype.slice.call | typeof Uint8Array.prototype.set.call} Getter */
30+
/** @type {{ [k in `\$${import('.').TypedArrayName}`]?: Getter } & { __proto__: null }} */
3031
var cache = { __proto__: null };
3132
if (hasToStringTag && gOPD && getPrototypeOf) {
3233
forEach(typedArrays, function (typedArray) {
@@ -55,13 +56,14 @@ if (hasToStringTag && gOPD && getPrototypeOf) {
5556
});
5657
}
5758

58-
/** @type {import('.')} */
59+
/** @type {(value: object) => false | import('.').TypedArrayName} */
5960
var tryTypedArrays = function tryAllTypedArrays(value) {
60-
/** @type {ReturnType<tryAllTypedArrays>} */ var found = false;
61+
/** @type {ReturnType<typeof tryAllTypedArrays>} */ var found = false;
6162
forEach(
6263
// eslint-disable-next-line no-extra-parens
63-
/** @type {Record<`\$${TypedArrayName}`, typeof cache>} */ /** @type {any} */ (cache),
64-
/** @type {(getter: typeof cache, name: `\$${TypedArrayName}`) => void} */ function (getter, typedArray) {
64+
/** @type {Record<`\$${TypedArrayName}`, Getter>} */ /** @type {any} */ (cache),
65+
/** @type {(getter: Getter, name: `\$${import('.').TypedArrayName}`) => void} */
66+
function (getter, typedArray) {
6567
if (!found) {
6668
try {
6769
// @ts-expect-error TODO: fix
@@ -75,16 +77,16 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
7577
return found;
7678
};
7779

78-
/** @type {import('.')} */
80+
/** @type {(value: object) => false | import('.').TypedArrayName} */
7981
var trySlices = function tryAllSlices(value) {
80-
/** @type {ReturnType<tryAllSlices>} */ var found = false;
82+
/** @type {ReturnType<typeof tryAllSlices>} */ var found = false;
8183
forEach(
8284
// eslint-disable-next-line no-extra-parens
83-
/** @type {any} */ (cache),
84-
/** @type {(getter: typeof cache, name: `\$${TypedArrayName}`) => void} */ function (getter, name) {
85+
/** @type {Record<`\$${TypedArrayName}`, Getter>} */ /** @type {any} */ (cache),
86+
/** @type {(getter: typeof cache, name: `\$${import('.').TypedArrayName}`) => void} */ function (getter, name) {
8587
if (!found) {
8688
try {
87-
// @ts-expect-error TODO: fix
89+
// @ts-expect-error TODO: fix
8890
getter(value);
8991
found = $slice(name, 1);
9092
} catch (e) { /**/ }
@@ -98,6 +100,7 @@ var trySlices = function tryAllSlices(value) {
98100
module.exports = function whichTypedArray(value) {
99101
if (!value || typeof value !== 'object') { return false; }
100102
if (!hasToStringTag) {
103+
/** @type {string} */
101104
var tag = $slice($toString(value), 8, -1);
102105
if ($indexOf(typedArrays, tag) > -1) {
103106
return tag;

test/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,10 @@ test('@@toStringTag', { skip: !hasToStringTag }, function (t) {
9090
t.end();
9191
});
9292

93-
/** @typedef {Int8ArrayConstructor | Uint8ArrayConstructor | Uint8ClampedArrayConstructor | Int16ArrayConstructor | Uint16ArrayConstructor | Int32ArrayConstructor | Uint32ArrayConstructor | Float32ArrayConstructor | Float64ArrayConstructor | BigInt64ArrayConstructor | BigUint64ArrayConstructor} TypedArrayConstructor */
94-
9593
test('Typed Arrays', function (t) {
9694
forEach(typedArrayNames, function (typedArray) {
9795
// @ts-expect-error TODO: fix
98-
/** @type {TypedArrayConstructor} */ var TypedArray = global[typedArray];
96+
/** @type {import('../').TypedArrayConstructor} */ var TypedArray = global[typedArray];
9997
if (isCallable(TypedArray)) {
10098
var arr = new TypedArray(10);
10199
t.equal(whichTypedArray(arr), typedArray, 'new ' + typedArray + '(10) is typed array of type ' + typedArray);

0 commit comments

Comments
 (0)