diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..74949b0 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,7 @@ +declare namespace isAsyncFunction { + type AsyncFunction = (...args: any[]) => Promise +} + +declare function isAsyncFunction(fn: unknown): fn is isAsyncFunction.AsyncFunction; + +export = isAsyncFunction; \ No newline at end of file diff --git a/index.js b/index.js index dcc6ac6..23a06ee 100644 --- a/index.js +++ b/index.js @@ -19,8 +19,11 @@ var getAsyncFunc = function () { // eslint-disable-line consistent-return } catch (e) { } }; + +/** @type {import('.').AsyncFunction | false} */ var AsyncFunction; +/** @type {import('.')} */ module.exports = function isAsyncFunction(fn) { if (typeof fn !== 'function') { return false; @@ -37,7 +40,8 @@ module.exports = function isAsyncFunction(fn) { } if (typeof AsyncFunction === 'undefined') { var asyncFunc = getAsyncFunc(); - AsyncFunction = asyncFunc ? getProto(asyncFunc) : false; + // eslint-disable-next-line no-extra-parens + AsyncFunction = asyncFunc ? /** @type {import('.').AsyncFunction} */ (getProto(asyncFunc)) : false; } return getProto(fn) === AsyncFunction; }; diff --git a/package.json b/package.json index 0405721..1750232 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "test:uglified": "node test/uglified", "posttest": "npx npm@\">= 10.2\" audit --production", "lint": "eslint --ext=js,mjs .", + "postlint": "tsc && attw -P", "version": "auto-changelog && git add CHANGELOG.md", "postversion": "auto-changelog && git add CHANGELOG.md && git commit --no-edit --amend && git tag -f \"v$(node -e \"console.log(require('./package.json').version)\")\"" }, @@ -46,7 +47,13 @@ "safe-regex-test": "^1.1.0" }, "devDependencies": { + "@arethetypeswrong/cli": "^0.17.2", "@ljharb/eslint-config": "^21.1.1", + "@ljharb/tsconfig": "^0.2.3", + "@types/for-each": "^0.3.3", + "@types/make-async-function": "^1.0.2", + "@types/make-generator-function": "^2.0.3", + "@types/tape": "^5.8.0", "auto-changelog": "^2.5.0", "encoding": "^0.1.13", "eslint": "=8.8.0", @@ -58,6 +65,7 @@ "nyc": "^10.3.2", "safe-publish-latest": "^2.0.0", "tape": "^5.9.0", + "typescript": "next", "uglify-register": "^1.0.1" }, "testling": { diff --git a/test/index.js b/test/index.js index ef6dce5..5de7a6d 100644 --- a/test/index.js +++ b/test/index.js @@ -69,6 +69,7 @@ test('returns false for generator functions', function (t) { test('returns false for non-async function with faked @@toStringTag', { skip: !hasToStringTag || asyncFuncs.length === 0 }, function (t) { var asyncFunc = asyncFuncs[0]; + /** @type {{ toString(): unknown; valueOf(): unknown; [Symbol.toStringTag]?: unknown }} */ var fakeAsyncFunction = { toString: function () { return String(asyncFunc); }, valueOf: function () { return asyncFunc; } diff --git a/test/uglified.js b/test/uglified.js index fd82b55..e178146 100644 --- a/test/uglified.js +++ b/test/uglified.js @@ -1,5 +1,6 @@ 'use strict'; +// @ts-expect-error require('uglify-register/api').register({ exclude: [/\/node_modules\//, /\/test\//], uglify: { mangle: true } diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..75e48fd --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "@ljharb/tsconfig", + "compilerOptions": { + "target": "ES2021", + "maxNodeModuleJsDepth": 0, + }, + "exclude": [ + "coverage", + ], +}