From e695f256a501ba762c0995992316a1c48c432e60 Mon Sep 17 00:00:00 2001 From: Lo Date: Wed, 20 Nov 2024 14:07:44 +0800 Subject: [PATCH] types: enable @typescripteslint/array-type (#36) --- .eslintrc | 5 ++++- src/array/find.ts | 4 ++-- src/array/removeArrayBlank.ts | 2 +- src/array/removeArrayEmpty.ts | 2 +- src/array/removeItem.ts | 2 +- src/array/toggleItem.ts | 2 +- src/general/isArray.ts | 2 +- src/general/isNonEmptyArray.ts | 2 +- src/number/clampArrayRange.ts | 2 +- src/util/getAllParentScroller.ts | 4 ++-- 10 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.eslintrc b/.eslintrc index 0f7e9f8..00445d5 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,5 +1,8 @@ { "root": true, "ignorePatterns": ["lib", "coverage", "docs/.vitepress/cache", "docs/.vitepress/dist"], - "extends": ["@varlet"] + "extends": ["@varlet"], + "rules": { + "@typescript-eslint/array-type": "error" + } } diff --git a/src/array/find.ts b/src/array/find.ts index 1328fa6..bf8d5d1 100644 --- a/src/array/find.ts +++ b/src/array/find.ts @@ -1,6 +1,6 @@ export function find( - arr: Array, - fn: (item: T, index: number, array: Array) => any, + arr: T[], + fn: (item: T, index: number, array: T[]) => any, from: 'start' | 'end' = 'start', ): [T, number] | [null, -1] { let i = from === 'start' ? 0 : arr.length - 1 diff --git a/src/array/removeArrayBlank.ts b/src/array/removeArrayBlank.ts index a552314..6562ec8 100644 --- a/src/array/removeArrayBlank.ts +++ b/src/array/removeArrayBlank.ts @@ -1,3 +1,3 @@ -export function removeArrayBlank(arr: Array) { +export function removeArrayBlank(arr: (T | null | undefined)[]) { return arr.filter((item) => item != null) as T[] } diff --git a/src/array/removeArrayEmpty.ts b/src/array/removeArrayEmpty.ts index 9bfd033..c7b902a 100644 --- a/src/array/removeArrayEmpty.ts +++ b/src/array/removeArrayEmpty.ts @@ -1,3 +1,3 @@ -export function removeArrayEmpty(arr: Array) { +export function removeArrayEmpty(arr: (T | null | undefined | '')[]) { return arr.filter((item) => item != null && item !== '') as T[] } diff --git a/src/array/removeItem.ts b/src/array/removeItem.ts index 3023883..cc3c880 100644 --- a/src/array/removeItem.ts +++ b/src/array/removeItem.ts @@ -1,4 +1,4 @@ -export function removeItem(arr: Array, item: T) { +export function removeItem(arr: T[], item: T) { if (arr.length) { const index: number = arr.indexOf(item) if (index > -1) { diff --git a/src/array/toggleItem.ts b/src/array/toggleItem.ts index 0284f3b..c16ac9b 100644 --- a/src/array/toggleItem.ts +++ b/src/array/toggleItem.ts @@ -1,6 +1,6 @@ import { removeItem } from './removeItem' -export function toggleItem(arr: Array, item: T) { +export function toggleItem(arr: T[], item: T) { arr.includes(item) ? removeItem(arr, item) : arr.push(item) return arr } diff --git a/src/general/isArray.ts b/src/general/isArray.ts index 28d5da1..ed5e976 100644 --- a/src/general/isArray.ts +++ b/src/general/isArray.ts @@ -1,3 +1,3 @@ -export function isArray(val: unknown): val is Array { +export function isArray(val: unknown): val is any[] { return Array.isArray(val) } diff --git a/src/general/isNonEmptyArray.ts b/src/general/isNonEmptyArray.ts index b9c453e..edb843f 100644 --- a/src/general/isNonEmptyArray.ts +++ b/src/general/isNonEmptyArray.ts @@ -1,5 +1,5 @@ import { isArray } from './isArray' -export function isNonEmptyArray(val: unknown): val is Array { +export function isNonEmptyArray(val: unknown): val is any[] { return isArray(val) && !!val.length } diff --git a/src/number/clampArrayRange.ts b/src/number/clampArrayRange.ts index 3acff65..d0304be 100644 --- a/src/number/clampArrayRange.ts +++ b/src/number/clampArrayRange.ts @@ -1,5 +1,5 @@ import { clamp } from './clamp' -export function clampArrayRange(index: number, arr: Array) { +export function clampArrayRange(index: number, arr: unknown[]) { return clamp(index, 0, arr.length - 1) } diff --git a/src/util/getAllParentScroller.ts b/src/util/getAllParentScroller.ts index a154ea0..38b10dc 100644 --- a/src/util/getAllParentScroller.ts +++ b/src/util/getAllParentScroller.ts @@ -1,8 +1,8 @@ import { isWindow } from '../general' import { getParentScroller } from './getParentScroller' -export function getAllParentScroller(el: HTMLElement): Array { - const allParentScroller: Array = [] +export function getAllParentScroller(el: HTMLElement): (HTMLElement | Window)[] { + const allParentScroller: (HTMLElement | Window)[] = [] let element: HTMLElement | Window = el while (!isWindow(element)) {