Skip to content

Commit 3144671

Browse files
committed
add types
1 parent 507b948 commit 3144671

File tree

5 files changed

+94
-3
lines changed

5 files changed

+94
-3
lines changed

index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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;
13+
14+
declare function typedArrayByteLength(value: TypedArray): number;
15+
declare function typedArrayByteLength(value: unknown): false;
16+
17+
export = typedArrayByteLength;

index.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,29 @@ 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 */
14+
15+
/** @type {Object.<TypedArrayNames, Getter>} */
1116
var getters = {};
1217

1318
var oDP = Object.defineProperty;
1419
if (gOPD) {
20+
/** @type {Getter} */
1521
var getByteLength = function (x) {
1622
return x.byteLength;
1723
};
1824
forEach(typedArrays, function (typedArray) {
1925
// In Safari 7, Typed Array constructors are typeof object
2026
if (typeof global[typedArray] === 'function' || typeof global[typedArray] === 'object') {
2127
var Proto = global[typedArray].prototype;
28+
// @ts-expect-error TS doesn't narrow properly inside callbacks
2229
var descriptor = gOPD(Proto, 'byteLength');
2330
if (!descriptor && hasProto) {
31+
// @ts-expect-error hush, TS, every object has a dunder proto
2432
var superProto = Proto.__proto__; // eslint-disable-line no-proto
33+
// @ts-expect-error TS doesn't narrow properly inside callbacks
2534
descriptor = gOPD(superProto, 'byteLength');
2635
}
2736
// Opera 12.16 has a magic byteLength data property on instances AND on Proto
@@ -30,6 +39,7 @@ if (gOPD) {
3039
} else if (oDP) {
3140
// this is likely an engine where instances have a magic byteLength data property
3241
var arr = new global[typedArray](2);
42+
// @ts-expect-error TS doesn't narrow properly inside callbacks
3343
descriptor = gOPD(arr, 'byteLength');
3444
if (descriptor && descriptor.configurable) {
3545
oDP(arr, 'length', { value: 3 });
@@ -42,9 +52,10 @@ if (gOPD) {
4252
});
4353
}
4454

55+
/** @type {Getter} */
4556
var tryTypedArrays = function tryAllTypedArrays(value) {
46-
var foundByteLength;
47-
forEach(getters, function (getter) {
57+
/** @type {number} */ var foundByteLength;
58+
forEach(getters, /** @type {(getter: Getter) => void} */ function (getter) {
4859
if (typeof foundByteLength !== 'number') {
4960
try {
5061
var byteLength = getter(value);
@@ -54,9 +65,11 @@ var tryTypedArrays = function tryAllTypedArrays(value) {
5465
} catch (e) {}
5566
}
5667
});
68+
// @ts-expect-error TS can't guarantee the callback is invoked sync
5769
return foundByteLength;
5870
};
5971

72+
/** @type {import('.')} */
6073
module.exports = function typedArrayByteLength(value) {
6174
if (!isTypedArray(value)) {
6275
return false;

package.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
".": "./index.js",
88
"./package.json": "./package.json"
99
},
10+
"types": "./index.d.ts",
1011
"scripts": {
1112
"prepack": "npmignore --auto --commentLines=autogenerated",
1213
"prepublishOnly": "safe-publish-latest",
1314
"prepublish": "not-in-publish || npm run prepublishOnly",
1415
"pretest": "npm run lint",
1516
"prelint": "evalmd README.md",
1617
"lint": "eslint --ext=js,mjs .",
18+
"postlint": "tsc -p .",
1719
"tests-only": "nyc tape 'test/**/*.js'",
1820
"test": "npm run tests-only",
1921
"posttest": "aud --production",
@@ -62,6 +64,14 @@
6264
},
6365
"devDependencies": {
6466
"@ljharb/eslint-config": "^21.1.0",
67+
"@types/call-bind": "^1.0.5",
68+
"@types/for-each": "^0.3.3",
69+
"@types/gopd": "^1.0.3",
70+
"@types/is-callable": "^1.1.2",
71+
"@types/make-arrow-function": "^1.2.2",
72+
"@types/make-generator-function": "^2.0.3",
73+
"@types/object-inspect": "^1.8.4",
74+
"@types/tape": "^5.6.4",
6575
"aud": "^2.0.4",
6676
"auto-changelog": "^2.4.0",
6777
"available-typed-arrays": "^1.0.7",
@@ -75,7 +85,8 @@
7585
"nyc": "^10.3.2",
7686
"object-inspect": "^1.13.1",
7787
"safe-publish-latest": "^2.0.0",
78-
"tape": "^5.7.5"
88+
"tape": "^5.7.5",
89+
"typescript": "next"
7990
},
8091
"engines": {
8192
"node": ">= 0.4"

test/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ var availableTypedArrays = require('available-typed-arrays')();
1111

1212
test('not arrays', function (t) {
1313
t.test('non-number/string primitives', function (st) {
14+
// @ts-expect-error
1415
st.equal(false, typedArrayByteLength(), 'undefined is not typed array');
1516
st.equal(false, typedArrayByteLength(null), 'null is not typed array');
1617
st.equal(false, typedArrayByteLength(false), 'false is not typed array');

tsconfig.json

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"compilerOptions": {
3+
/* Visit https://aka.ms/tsconfig to read more about this file */
4+
5+
/* Projects */
6+
7+
/* Language and Environment */
8+
"target": "ESNext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
9+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
10+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
11+
"useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
12+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
13+
14+
/* Modules */
15+
"module": "commonjs", /* Specify what module code is generated. */
16+
// "rootDir": "./", /* Specify the root folder within your source files. */
17+
// "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */
18+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
19+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
20+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
21+
"typeRoots": ["types"], /* Specify multiple folders that act like './node_modules/@types'. */
22+
"resolveJsonModule": true, /* Enable importing .json files. */
23+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
24+
25+
/* JavaScript Support */
26+
"allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
27+
"checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
28+
"maxNodeModuleJsDepth": 0, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
29+
30+
/* Emit */
31+
"declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
32+
"declarationMap": true, /* Create sourcemaps for d.ts files. */
33+
"noEmit": true, /* Disable emitting files from a compilation. */
34+
35+
/* Interop Constraints */
36+
"allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
37+
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
38+
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
39+
40+
/* Type Checking */
41+
"strict": true, /* Enable all strict type-checking options. */
42+
43+
/* Completeness */
44+
//"skipLibCheck": true /* Skip type checking all .d.ts files. */
45+
},
46+
"exclude": [
47+
"coverage"
48+
]
49+
}

0 commit comments

Comments
 (0)