From 7ce3a5b7c166c5309bb98ceb7e389fbc0d0ec0ad Mon Sep 17 00:00:00 2001 From: Gaic4o Date: Sun, 9 Jun 2024 13:10:19 +0900 Subject: [PATCH] fix: use UnknownMap type for better type safety in utility functions --- .changeset/late-buttons-fail.md | 5 +++++ packages/core/theme/src/utils/object.ts | 17 +++++++---------- 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 .changeset/late-buttons-fail.md diff --git a/.changeset/late-buttons-fail.md b/.changeset/late-buttons-fail.md new file mode 100644 index 0000000000..e7e69b689f --- /dev/null +++ b/.changeset/late-buttons-fail.md @@ -0,0 +1,5 @@ +--- +"@nextui-org/theme": patch +--- + +WHAT: Used `@ts-ignore` to bypass type checking, Functions used `Record` or `Object`, causing potential type issues and limited flexibility. Functions now use `UnknownMap` type for better type safety and flexibility. diff --git a/packages/core/theme/src/utils/object.ts b/packages/core/theme/src/utils/object.ts index c320764cd0..5a21c14c9b 100644 --- a/packages/core/theme/src/utils/object.ts +++ b/packages/core/theme/src/utils/object.ts @@ -1,7 +1,9 @@ import flatten from "flat"; -export function swapColorValues(colors: T) { - const swappedColors = {}; +type UnknownMap = Record; + +export function swapColorValues(colors: T) { + const swappedColors: UnknownMap = {}; const keys = Object.keys(colors); const length = keys.length; @@ -9,31 +11,26 @@ export function swapColorValues(colors: T) { const key1 = keys[i]; const key2 = keys[length - 1 - i]; - // @ts-ignore swappedColors[key1] = colors[key2]; - // @ts-ignore swappedColors[key2] = colors[key1]; } if (length % 2 !== 0) { const middleKey = keys[Math.floor(length / 2)]; - // @ts-ignore swappedColors[middleKey] = colors[middleKey]; } return swappedColors; } -export function removeDefaultKeys(obj: T) { - const newObj = {}; +export function removeDefaultKeys(obj: T): UnknownMap { + const newObj: UnknownMap = {}; for (const key in obj) { if (key.endsWith("-DEFAULT")) { - // @ts-ignore newObj[key.replace("-DEFAULT", "")] = obj[key]; continue; } - // @ts-ignore newObj[key] = obj[key]; } @@ -52,5 +49,5 @@ export const flattenThemeObject = (obj: TTarget) => flatten(obj, { safe: true, delimiter: "-", - }) as Object, + }) as UnknownMap, );