From aea15f92c387f164c87117d6a9786c646efa62ac Mon Sep 17 00:00:00 2001 From: Krabba <11424371+Krabba@users.noreply.github.com> Date: Tue, 22 Oct 2024 17:54:01 +0200 Subject: [PATCH] Update types for variants --- src/Theme.ts | 13 ++++++++----- src/types.ts | 34 +++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/Theme.ts b/src/Theme.ts index 49f6aa5..77b56e0 100644 --- a/src/Theme.ts +++ b/src/Theme.ts @@ -20,7 +20,7 @@ import { camelToKebab } from "./utils/camelToKebab"; * * TODO: consider exposing a user-facing API for configuring this themePropertiesConfig; users could create their own ThemeProperty sub-classes (like ColorProperty) to write their own value conversion logic.. would make tailwind-easy-theme incredibly flexible -- like more of a low-level theming framework */ -const themePropertiesConfig: InternalThemePropertiesConfig = { +const themePropertiesConfig: InternalThemePropertiesConfig = { colors: { prefix: "", type: ColorProperty, @@ -85,7 +85,7 @@ const themePropertiesConfig: InternalThemePropertiesConfig = { prefix: "stroke", type: ColorProperty, }, -}; +} as const; export class Theme { private userPrefix: string | undefined; @@ -115,9 +115,12 @@ export class Theme { let Property = ThemeProperty; if (propertyKey in themePropertiesConfig) { - const config = themePropertiesConfig[propertyKey]; - prefix = config.prefix ?? prefix; - Property = config.type ?? Property; + const config = + themePropertiesConfig[ + propertyKey as keyof typeof themePropertiesConfig + ]; + prefix = config?.prefix ?? prefix; + Property = config?.type ?? Property; } prefix = userPrefix ? `${userPrefix}-${prefix}` : prefix; diff --git a/src/types.ts b/src/types.ts index d4586f3..d27e9f0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -39,10 +39,34 @@ export type ThemePropertyConfig = Record< string, FlatThemePropertyConfig | string >; -export type PartialThemePropertyConfig< - PrimaryTheme extends ThemePropertyConfig + +type HexCode = string | undefined; + +type RecursiveOptionalKeyValuePair< + K extends string | number | symbol, + V extends string | object > = { - [K in keyof PrimaryTheme]?: Partial; + [Key in K]?: V extends string + ? HexCode + : V extends object + ? V[keyof V] extends string | object + ? RecursiveOptionalKeyValuePair + : never + : never; +}; + +export type PartialThemePropertyConfig = { + [K in keyof PrimaryTheme]?: PrimaryTheme[K] extends infer PrimaryThemeKey + ? { + [Key in keyof PrimaryThemeKey]?: PrimaryThemeKey[Key] extends infer Value + ? Value extends object + ? Value[keyof Value] extends string | object + ? RecursiveOptionalKeyValuePair + : never + : HexCode + : never; + } + : never; }; export type ThemePropertyOptions = { @@ -52,8 +76,8 @@ export type ThemePropertyOptions = { // Constructor signature for classes extending ThemeProperty export type ThemePropertyConstructor = new (...args: any[]) => ThemeProperty; -export type InternalThemePropertiesConfig = { - [P: keyof ThemeConfig]: { +export type InternalThemePropertiesConfig = { + [P in keyof T]?: { prefix: string; type: ThemePropertyConstructor; };