From b8b9c8b7054196813ee05775c7adfb8d4c79e1aa Mon Sep 17 00:00:00 2001 From: Josh Ellis Date: Fri, 15 Jan 2021 14:46:47 +0000 Subject: [PATCH] refactor: reduce eslint-warnings & type changes --- src/helpers/act.ts | 16 +++++++--------- src/helpers/promises.ts | 6 +++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/helpers/act.ts b/src/helpers/act.ts index 0f8acea4..895560d9 100644 --- a/src/helpers/act.ts +++ b/src/helpers/act.ts @@ -1,21 +1,19 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -/* eslint-disable @typescript-eslint/no-unsafe-assignment */ -/* eslint-disable @typescript-eslint/no-unsafe-member-access */ import { Act } from '../types/react' import { suppressErrorOutput } from './console' +import { isPromise } from './promises' + function createActWrapper(baseAct: Act) { - const act: Act = async (callback: () => any) => { + const act: Act = async (callback: () => unknown) => { const restoreOutput = suppressErrorOutput() try { let awaitRequired = false - const actResult = (baseAct(() => { + const actResult = baseAct(() => { const callbackResult = callback() - awaitRequired = callbackResult !== undefined && !!callbackResult.then - return callbackResult - }) as any) as PromiseLike - + awaitRequired = isPromise(callbackResult) + return callbackResult as Promise + }) return awaitRequired ? await actResult : undefined } finally { restoreOutput() diff --git a/src/helpers/promises.ts b/src/helpers/promises.ts index 2fa89e5f..a3a59e76 100644 --- a/src/helpers/promises.ts +++ b/src/helpers/promises.ts @@ -7,4 +7,8 @@ async function callAfter(callback: () => void, ms: number) { callback() } -export { resolveAfter, callAfter } +function isPromise(value: unknown): boolean { + return value !== undefined && typeof (value as PromiseLike).then === 'function' +} + +export { resolveAfter, callAfter, isPromise }