TypeScript error in custom tv
utility when merging configurations
#219
Labels
bug
Something isn't working
tv
utility when merging configurations
#219
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.
The text was updated successfully, but these errors were encountered: