nextui-org / tailwind-variants

🦄 Tailwindcss first-class variant API
https://tailwind-variants.org
MIT License
2.42k stars 68 forks source link

TypeScript error in custom `tv` utility when merging configurations #219

Open pavelnaiya opened 1 month ago

pavelnaiya commented 1 month ago

Describe the bug When using the custom tv utility function from src/utils/tv.ts, TypeScript is reporting a type error. The error seems to be related to the configuration object passed to the tvBase function, specifically when merging custom configurations with the base configuration.

To Reproduce

  1. Define a custom twMergeConfig in tw-merge-config.ts
  2. Create a custom tv function in tv.ts that extends the base tvBase function
  3. Use the custom tv function in your code
  4. TypeScript reports a complex type error

Code sample typescript:tw-merge-config.ts import type { Config } from "tailwind-merge";

const BORDER_SCALE = [ "none", "DEFAULT", "xs", "sm", "md", "lg", "xl", "full", ] as const; const COMMON_SCALE = [ "50", "100", "200", "300", "400", "500", "600", "700", "800", "900", "950", ] as const;

// regex value for number pattern const num = (classPart: string): boolean => /^\d+$/.test(classPart);

export const twMergeConfig: Partial = { theme: { colors: [ { primary: COMMON_SCALE }, { secondary: COMMON_SCALE }, { canvas: COMMON_SCALE }, { content: COMMON_SCALE }, { success: COMMON_SCALE }, { danger: COMMON_SCALE }, { alert: COMMON_SCALE }, { info: COMMON_SCALE }, ], borderWidth: BORDER_SCALE, }, classGroups: { "font-size": [ { text: [ { display: [num] }, { heading: [num] }, { body: [num] }, { label: [num] }, { caption: [num] }, ], }, ], }, };

import { tv as tvBase, TV } from "tailwind-variants";

import { twMergeConfig } from "./tw-merge-config";

export const tv: TV = (options, config) => tvBase(options, { ...config, twMerge: config?.twMerge ?? true, twMergeConfig: { ...config.twMergeConfig, theme: { ...config.twMergeConfig?.theme, ...twMergeConfig.theme, }, classGroups: { ...config.twMergeConfig?.classGroups, ...twMergeConfig.classGroups, }, }, });

// Re-export types from tailwind-variants for convenience export type { VariantProps } from "tailwind-variants";

Error message The error message is complex and lengthy, but the key points are:

  1. There's a type mismatch between the return type of the custom tv function and the expected TVReturnType.
  2. The error suggests issues with merging configurations, particularly with twMergeConfig.
  3. The type system is having trouble reconciling the extended configuration with the base types from tailwind-variants.

Environment

Additional context The error seems to stem from the way the custom configuration is being merged with the base configuration. The type system is struggling to infer the correct types for the merged configuration object.