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
Define a custom twMergeConfig in tw-merge-config.ts
Create a custom tv function in tv.ts that extends the base tvBase function
Use the custom tv function in your code
TypeScript reports a complex type error
Code sample
typescript:tw-merge-config.ts
import type { Config } from "tailwind-merge";
// 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:
There's a type mismatch between the return type of the custom tv function and the expected TVReturnType.
The error suggests issues with merging configurations, particularly with twMergeConfig.
The type system is having trouble reconciling the extended configuration with the base types from tailwind-variants.
Environment
TypeScript: ^5.6.2
Next.js: ^14.1.4
tailwind-variants: ^0.2.1
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.
Describe the bug When using the custom
tv
utility function fromsrc/utils/tv.ts
, TypeScript is reporting a type error. The error seems to be related to the configuration object passed to thetvBase
function, specifically when merging custom configurations with the base configuration.To Reproduce
twMergeConfig
intw-merge-config.ts
tv
function intv.ts
that extends the basetvBase
functiontv
function in your codeCode 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:
tv
function and the expectedTVReturnType
.twMergeConfig
.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.