From 83ae7f32a2a34d40706200fe36915a79db265252 Mon Sep 17 00:00:00 2001 From: Gaic4o Date: Sun, 9 Jun 2024 22:00:32 +0900 Subject: [PATCH 1/2] 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..ece881b2a2 --- /dev/null +++ b/.changeset/late-buttons-fail.md @@ -0,0 +1,5 @@ +--- +"@nextui-org/theme": patch +--- + +Used `@ts-ignore` to bypass type checking, and used `Object`, leading to potential type problems and limited flexibility. The function now uses the `StringMap` 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..122312a817 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 StringMap = Record; + +export function swapColorValues(colors: T) { + const swappedColors: StringMap = {}; 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): StringMap { + const newObj: StringMap = {}; 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 StringMap, ); From af990aef0d7df1a5b2cbb68ec0a9edaad0eced9a Mon Sep 17 00:00:00 2001 From: Minsu Date: Sun, 8 Sep 2024 23:38:37 +0900 Subject: [PATCH 2/2] fix(theme): updates the existing string to ColorMap type including boolean value --- packages/core/theme/src/utils/object.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/core/theme/src/utils/object.ts b/packages/core/theme/src/utils/object.ts index 122312a817..412b184aa5 100644 --- a/packages/core/theme/src/utils/object.ts +++ b/packages/core/theme/src/utils/object.ts @@ -1,9 +1,10 @@ import flatten from "flat"; -type StringMap = Record; +type ColorValue = string | boolean; +type ColorMap = Record; -export function swapColorValues(colors: T) { - const swappedColors: StringMap = {}; +export function swapColorValues(colors: T) { + const swappedColors: ColorMap = {}; const keys = Object.keys(colors); const length = keys.length; @@ -23,8 +24,8 @@ export function swapColorValues(colors: T) { return swappedColors; } -export function removeDefaultKeys(obj: T): StringMap { - const newObj: StringMap = {}; +export function removeDefaultKeys(obj: T): ColorMap { + const newObj: ColorMap = {}; for (const key in obj) { if (key.endsWith("-DEFAULT")) { @@ -49,5 +50,5 @@ export const flattenThemeObject = (obj: TTarget) => flatten(obj, { safe: true, delimiter: "-", - }) as StringMap, + }) as ColorMap, );