Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeScript error in custom tv utility when merging configurations #219

Open
pavelnaiya opened this issue Oct 14, 2024 · 0 comments
Open
Labels
bug Something isn't working

Comments

@pavelnaiya
Copy link

pavelnaiya commented Oct 14, 2024

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

  • 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.

@pavelnaiya pavelnaiya added the bug Something isn't working label Oct 14, 2024
@pavelnaiya pavelnaiya changed the title Getting type error when extending Getting type error when extending tv Oct 14, 2024
@pavelnaiya pavelnaiya changed the title Getting type error when extending tv TypeScript error in custom tv utility when merging configurations Oct 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant