Skip to content

Commit b25694f

Browse files
committed
fix: improved typing for this code
1 parent 66b8a1e commit b25694f

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

packages/jacaranda/src/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ type VariantOptions<V> = {
6060
};
6161
};
6262

63+
// BooleanVariantKey type for 'true' and 'false' string literals
64+
type BooleanVariantKey = 'true' | 'false';
65+
6366
type CompoundVariant<V> = {
6467
variants: Partial<{
6568
[P in keyof V]: keyof V[P] | boolean;
@@ -88,7 +91,7 @@ export type OmitUndefined<T> = T extends undefined ? never : T;
8891
type OptionalIfHasDefault<Props, Defaults> = Omit<Props, keyof Defaults> &
8992
Partial<Pick<Props, Extract<keyof Defaults, keyof Props>>>;
9093

91-
export type VariantProps<Component extends (...args: any) => any> = Omit<
94+
export type VariantProps<Component extends <T>(arg: T) => ReturnType<Component>> = Omit<
9295
OmitUndefined<Parameters<Component>[0]>,
9396
'style'
9497
>;
@@ -114,12 +117,12 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
114117
} as VariantProps;
115118

116119
// Apply variant styles
117-
for (const [propName, value] of Object.entries(mergedProps) as [keyof V, any][]) {
120+
for (const [propName, value] of Object.entries(mergedProps) as [keyof V, keyof VariantProps[keyof V] | boolean][]) {
118121
const variantGroup = config.variants[propName];
119122
if (variantGroup) {
120123
// Handle boolean variants
121124
if (typeof value === 'boolean') {
122-
const booleanValue = value ? 'true' : 'false';
125+
const booleanValue: BooleanVariantKey = value ? 'true' : 'false';
123126
if (variantGroup[booleanValue as keyof typeof variantGroup]) {
124127
styles = {
125128
...styles,
@@ -144,7 +147,7 @@ function styles<V extends VariantOptions<V>>(config: VariantStyleConfig<V>) {
144147
for (const compound of config.compoundVariants) {
145148
if (
146149
Object.entries(compound.variants).every(
147-
([propName, value]: [string, unknown]) => {
150+
([propName, value]) => {
148151
// Handle boolean values in compound variants
149152
if (typeof value === 'boolean') {
150153
return mergedProps[propName as keyof V] === value;
@@ -188,7 +191,7 @@ interface CreateTokensReturn {
188191

189192
// Helper to resolve token references in style objects
190193
function resolveTokens(style: StyleObject, tokens: TokenConfig): StyleObject {
191-
return Object.entries(style).reduce<Record<string, any>>((acc, [key, value]) => {
194+
return Object.entries(style).reduce<Record<string, ResolvedStyle[keyof ResolvedStyle]>>((acc, [key, value]) => {
192195
if (typeof value !== 'string' || !value.startsWith('$')) {
193196
acc[key] = value;
194197
return acc;
@@ -225,22 +228,28 @@ export function defineTokens<T extends TokenConfig>(tokenConfig: T): CreateToken
225228

226229
// Resolve tokens in variants
227230
const resolvedVariants = config.variants
228-
? Object.entries(config.variants).reduce((acc, [key, variantGroup]: [string, any]) => {
229-
const resolvedGroup = Object.entries(variantGroup).reduce(
230-
(groupAcc, [variantKey, styles]: [string, any]) => {
231+
? Object.entries(config.variants).reduce<Partial<V>>((acc, [key, variantGroup]) => {
232+
type VariantGroupType = Record<string, StyleObject>;
233+
234+
const resolvedGroup = Object.entries(variantGroup as VariantGroupType).reduce<Record<string, StyleObject>>(
235+
(groupAcc, [variantKey, variantStyles]) => {
231236
return {
232237
...groupAcc,
233-
[variantKey]: resolveTokens(styles, tokens),
238+
[variantKey]: resolveTokens(variantStyles, tokens),
234239
};
235240
},
236-
{},
241+
{}
237242
);
238-
return { ...acc, [key]: resolvedGroup };
243+
244+
return {
245+
...acc,
246+
[key as keyof V]: resolvedGroup as V[keyof V]
247+
};
239248
}, {}) as V
240249
: {} as V;
241250

242251
// Resolve tokens in compound variants
243-
const resolvedCompoundVariants = config.compoundVariants?.map((compound: any) => ({
252+
const resolvedCompoundVariants = config.compoundVariants?.map((compound) => ({
244253
...compound,
245254
style: resolveTokens(compound.style, tokens),
246255
}));

0 commit comments

Comments
 (0)