From 42e29a8a57ce641e74bb3b5a09358e129e7751fa Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Thu, 12 Dec 2024 16:16:20 -0800 Subject: [PATCH] [New] add types --- index.d.ts | 9 +++++++++ index.js | 5 +++-- package.json | 10 ++++++++-- test/index.js | 4 ++-- tsconfig.json | 9 +++++++++ types/array.prototype.some/index.d.ts | 9 +++++++++ 6 files changed, 40 insertions(+), 6 deletions(-) create mode 100644 index.d.ts create mode 100644 tsconfig.json create mode 100644 types/array.prototype.some/index.d.ts diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..e347cf7 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,9 @@ +import whichTypedArray from 'which-typed-array'; + +declare namespace hasTypedArrays { + type TypedArrayName = whichTypedArray.TypedArrayName; +} + +declare function hasTypedArrays(): boolean; + +export = hasTypedArrays; \ No newline at end of file diff --git a/index.js b/index.js index 89decfb..ffdeda3 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,7 @@ var some = require('array.prototype.some'); var whichTypedArray = require('which-typed-array'); +/** @type {import('.').TypedArrayName[]} */ var typedArrays = [ 'Float32Array', 'Float64Array', @@ -18,7 +19,7 @@ var typedArrays = [ ]; module.exports = function hasTypedArrays() { - return some(typedArrays, function (TA) { - return whichTypedArray(new global[TA]()); + return some(typedArrays, /** @param {import('.').TypedArrayName} TA */ function (TA) { + return !!whichTypedArray(new global[TA]()); }); }; diff --git a/package.json b/package.json index aa7724d..81a346c 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "prepublishOnly": "safe-publish-latest", "prepublish": "not-in-publish || npm run prepublishOnly", "lint": "eslint --ext=js,mjs .", + "postlint": "tsc && attw -P", "pretest": "npm run lint", "tests-only": "nyc tape 'test/**/*.js'", "test:harmony": "nyc node --harmony --es-staging test", @@ -48,7 +49,10 @@ }, "homepage": "https://github.com/inspect-js/has-typed-arrays#readme", "devDependencies": { + "@arethetypeswrong/cli": "^0.17.1", "@ljharb/eslint-config": "^21.1.1", + "@ljharb/tsconfig": "^0.2.2", + "@types/tape": "^5.6.5", "auto-changelog": "^2.5.0", "encoding": "^0.1.13", "eslint": "=8.8.0", @@ -56,7 +60,8 @@ "npmignore": "^0.3.1", "nyc": "^10.3.2", "safe-publish-latest": "^2.0.0", - "tape": "^5.9.0" + "tape": "^5.9.0", + "typescript": "next" }, "funding": { "url": "https://github.com/sponsors/ljharb" @@ -75,7 +80,8 @@ }, "publishConfig": { "ignore": [ - ".github/workflows" + ".github/workflows", + "types" ] } } diff --git a/test/index.js b/test/index.js index 5814586..83d6180 100644 --- a/test/index.js +++ b/test/index.js @@ -11,7 +11,7 @@ test('export', function (t) { }); test('Typed Arrays', function (t) { - var anyExist = some([ + var anyExist = some(/** @type {import('..').TypedArrayName[]} */ [ 'Float32Array', 'Float64Array', 'Int8Array', @@ -23,7 +23,7 @@ test('Typed Arrays', function (t) { 'Uint32Array', 'BigInt64Array', 'BigUint64Array' - ], function (TA) { + ], /** @param {import('..').TypedArrayName} TA */ function (TA) { return typeof global[TA] === 'function'; }); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d1f4f9f --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "extends": "@ljharb/tsconfig", + "compilerOptions": { + "maxNodeModuleJsDepth": 0, + }, + "exclude": [ + "coverage" + ] +} diff --git a/types/array.prototype.some/index.d.ts b/types/array.prototype.some/index.d.ts new file mode 100644 index 0000000..d68a094 --- /dev/null +++ b/types/array.prototype.some/index.d.ts @@ -0,0 +1,9 @@ +declare module 'array.prototype.some' { + function some( + arr: T[], + callbackfn: (value: T, index?: number, array?: T[]) => boolean, + thisArg?: unknown, + ): boolean; + + export = some; +}