From 9a92ca766cfdd41560e21adffcb7202c945fcb4d Mon Sep 17 00:00:00 2001 From: Masaki Morishita Date: Tue, 23 Dec 2025 19:01:38 +0900 Subject: [PATCH] improve types for slots --- src/index.ts | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/index.ts b/src/index.ts index 04bce82..8ac8ed9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -160,7 +160,7 @@ function isSlotAwareValue(value: unknown): value is SlotAwareObject { } function addSlotClasses( - slotParts: Record, + slotParts: Record, slots: Record, ): void { for (const [slotName, slotClasses] of Object.entries(slots)) { @@ -174,7 +174,7 @@ function addSlotClasses( function processTraits>( traits: TTraits, propsTraits?: Props<{}, TTraits>["traits"], - slotParts?: Record, + slotParts?: Record, ): ClassValue[] { if (!propsTraits) return []; @@ -268,6 +268,8 @@ export function windctrl< ): ( props?: Props, ) => Result> { + type TSlotKeys = SlotKeys; + const { base, variants = {} as TVariants, @@ -290,7 +292,7 @@ export function windctrl< return (props = {} as Props) => { const classNameParts: ClassValue[] = []; let mergedStyle: CSSProperties = {}; - const slotParts: Record = {}; + const slotParts: Partial> = {}; // Priority order: Base < Variants < Traits < Dynamic // (Higher priority classes are added later, so tailwind-merge will keep them) @@ -353,18 +355,24 @@ export function windctrl< const hasStyle = Object.keys(mergedStyle).length > 0; - let finalSlots: Record | undefined; - const slotNames = Object.keys(slotParts); + let finalSlots: Partial> | undefined; + + const slotNames = Object.keys(slotParts) as TSlotKeys[]; if (slotNames.length > 0) { - finalSlots = {}; + const out: Partial> = {}; + for (const slotName of slotNames) { - const merged = twMerge(clsx(slotParts[slotName])); + const parts = slotParts[slotName]; + if (!parts) continue; + + const merged = twMerge(clsx(parts)); if (merged) { - finalSlots[slotName] = merged; + out[slotName] = merged; } } - if (Object.keys(finalSlots).length === 0) { - finalSlots = undefined; + + if (Object.keys(out).length > 0) { + finalSlots = out; } } @@ -372,7 +380,7 @@ export function windctrl< className: finalClassName, ...(hasStyle && { style: mergedStyle }), ...(finalSlots && { slots: finalSlots }), - } as Result>; + }; }; }