From e2aed2e9467c09fd8e32d8f4706289e4dc61bf2c Mon Sep 17 00:00:00 2001 From: luis angel lopez huari <156825396+luislh-dev@users.noreply.github.com> Date: Sun, 31 Aug 2025 07:22:47 -0500 Subject: [PATCH] refactor: improve type safety (#5539) * refactor(popover): consolidate imports from aria-utils module * refactor(popover): remove unnecessary type assertion for child ref * refactor: remove unnecessary type assertions and improve type safety * chore(changeset): add changeset --- .changeset/healthy-bottles-search.md | 10 ++++++++++ apps/docs/components/sandpack/use-sandpack.ts | 2 +- .../components/date-picker/src/use-date-picker-base.ts | 2 +- packages/components/form/src/utils.ts | 2 +- packages/components/popover/src/popover-trigger.tsx | 2 +- packages/components/popover/src/use-popover.ts | 3 +-- packages/hooks/use-aria-accordion-item/src/index.ts | 2 +- .../utilities/shared-utils/src/common/functions.ts | 4 ++-- packages/utilities/test-utils/src/mocks/image.ts | 2 +- 9 files changed, 19 insertions(+), 10 deletions(-) create mode 100644 .changeset/healthy-bottles-search.md diff --git a/.changeset/healthy-bottles-search.md b/.changeset/healthy-bottles-search.md new file mode 100644 index 000000000..4fae5402d --- /dev/null +++ b/.changeset/healthy-bottles-search.md @@ -0,0 +1,10 @@ +--- +"@heroui/use-aria-accordion-item": patch +"@heroui/date-picker": patch +"@heroui/shared-utils": patch +"@heroui/test-utils": patch +"@heroui/popover": patch +"@heroui/form": patch +--- + +Code quality improvements: removed unnecessary type assertions across multiple components and utilities, consolidated imports in Popover module, and enhanced type safety. diff --git a/apps/docs/components/sandpack/use-sandpack.ts b/apps/docs/components/sandpack/use-sandpack.ts index 45b9cbcaa..1d176c0c9 100644 --- a/apps/docs/components/sandpack/use-sandpack.ts +++ b/apps/docs/components/sandpack/use-sandpack.ts @@ -206,7 +206,7 @@ export const useSandpack = ({ "react": "18.3.1", "react-dom": "18.3.1", ${Object.entries( - omit(dependencies as any, [ + omit(dependencies, [ "react", "react-dom", "react-dom/client", diff --git a/packages/components/date-picker/src/use-date-picker-base.ts b/packages/components/date-picker/src/use-date-picker-base.ts index 519258e71..e244952dc 100644 --- a/packages/components/date-picker/src/use-date-picker-base.ts +++ b/packages/components/date-picker/src/use-date-picker-base.ts @@ -169,7 +169,7 @@ export function useDatePickerBase(originalProps: UseDatePic const disableAnimation = originalProps.disableAnimation ?? globalContext?.disableAnimation ?? false; - let stringFormatter = useLocalizedStringFormatter(intlMessages) as any; + let stringFormatter = useLocalizedStringFormatter(intlMessages); const isDefaultColor = originalProps.color === "default" || !originalProps.color; const hasMultipleMonths = visibleMonths > 1; diff --git a/packages/components/form/src/utils.ts b/packages/components/form/src/utils.ts index 7cdf31164..a3c57a0e0 100644 --- a/packages/components/form/src/utils.ts +++ b/packages/components/form/src/utils.ts @@ -136,7 +136,7 @@ export function useContextProps( ): [T, RefObject] { let ctx = useSlottedContext(context, props.slot) || {}; // @ts-ignore - TS says "Type 'unique symbol' cannot be used as an index type." but not sure why. - let {ref: contextRef, ...contextProps} = ctx as any; + let {ref: contextRef, ...contextProps} = ctx; let mergedRef = useObjectRef(useMemo(() => mergeRefs(ref, contextRef), [ref, contextRef])); let mergedProps = mergeProps(contextProps, props) as unknown as T; diff --git a/packages/components/popover/src/popover-trigger.tsx b/packages/components/popover/src/popover-trigger.tsx index cf05b46cb..9662a82e8 100644 --- a/packages/components/popover/src/popover-trigger.tsx +++ b/packages/components/popover/src/popover-trigger.tsx @@ -32,7 +32,7 @@ const PopoverTrigger = (props: PopoverTriggerProps) => { // Accessing the ref from props, else fallback to element.ref // https://github.com/facebook/react/pull/28348 - const childRef = child.props.ref ?? (child as any).ref; + const childRef = child.props.ref ?? child.ref; const {onPress, isDisabled, ...restProps} = useMemo(() => { return getTriggerProps(mergeProps(otherProps, child.props), childRef); diff --git a/packages/components/popover/src/use-popover.ts b/packages/components/popover/src/use-popover.ts index 145080a37..bb28a8745 100644 --- a/packages/components/popover/src/use-popover.ts +++ b/packages/components/popover/src/use-popover.ts @@ -13,9 +13,8 @@ import {useDOMRef} from "@heroui/react-utils"; import {useOverlayTriggerState} from "@react-stately/overlays"; import {useFocusRing} from "@react-aria/focus"; import {useOverlayTrigger, usePreventScroll} from "@react-aria/overlays"; -import {getShouldUseAxisPlacement} from "@heroui/aria-utils"; +import {getShouldUseAxisPlacement, getArrowPlacement} from "@heroui/aria-utils"; import {mapPropsVariants, useProviderContext} from "@heroui/system"; -import {getArrowPlacement} from "@heroui/aria-utils"; import {popover} from "@heroui/theme"; import {clsx, dataAttr, objectToDeps, mergeProps, mergeRefs} from "@heroui/shared-utils"; import {useMemo, useCallback, useRef} from "react"; diff --git a/packages/hooks/use-aria-accordion-item/src/index.ts b/packages/hooks/use-aria-accordion-item/src/index.ts index e4a067ed6..7784f85aa 100644 --- a/packages/hooks/use-aria-accordion-item/src/index.ts +++ b/packages/hooks/use-aria-accordion-item/src/index.ts @@ -49,7 +49,7 @@ export function useAriaAccordionItem( if (!manager.canSelectItem(key)) { return; } - manager.select(key, e as any); + manager.select(key, e); state.toggleKey(key); }, [key, manager], diff --git a/packages/utilities/shared-utils/src/common/functions.ts b/packages/utilities/shared-utils/src/common/functions.ts index d51870c22..78a0feae5 100644 --- a/packages/utilities/shared-utils/src/common/functions.ts +++ b/packages/utilities/shared-utils/src/common/functions.ts @@ -353,9 +353,9 @@ export const intersectionBy = (...args: [...arrays: T[][], iteratee: Iteratee const getIterateeValue = (item: T): unknown => { if (typeof iteratee === "function") { - return (iteratee as (value: T) => any)(item); + return iteratee(item); } else if (typeof iteratee === "string") { - return (item as any)[iteratee]; + return item[iteratee]; } else { throw new Error("Iteratee must be a function or a string key of the array elements"); } diff --git a/packages/utilities/test-utils/src/mocks/image.ts b/packages/utilities/test-utils/src/mocks/image.ts index b5932ef35..ab6379308 100644 --- a/packages/utilities/test-utils/src/mocks/image.ts +++ b/packages/utilities/test-utils/src/mocks/image.ts @@ -22,7 +22,7 @@ export function mockImage() { return name in this; } getAttribute(name: string) { - return name in this ? (this as any)[name] : null; + return name in this ? this[name] : null; } constructor() { setTimeout(() => {