|
1 | | -import { clsx, type ClassValue } from 'clsx' |
| 1 | +import { clsx } from 'clsx' |
2 | 2 | import { twMerge } from 'tailwind-merge' |
3 | 3 |
|
4 | | -export function cn(...inputs: ClassValue[]) { |
| 4 | +// Stricter than `ClassValue` from clsx |
| 5 | +export type ClassValueStrict = ClassArrayStrict | ClassDictionaryStrict | string | null | false | undefined |
| 6 | +export type ClassDictionaryStrict = Record<string, boolean | null | undefined> |
| 7 | +export type ClassArrayStrict = ClassValueStrict[] |
| 8 | + |
| 9 | +export function cn(...inputs: ClassValueStrict[]) { |
5 | 10 | return twMerge(clsx(inputs)) |
6 | 11 | } |
| 12 | + |
| 13 | +{ |
| 14 | + // Type-only test: |
| 15 | + const condition = false |
| 16 | + // `clsx` is not strict enough |
| 17 | + clsx('class1', 2, condition && 'no3', { class4: true, class5: 5, class6: [{ why: '?' }] }) |
| 18 | + // @ts-expect-error `cn` should be stricter and disallow this |
| 19 | + cn('class1', 2, condition && 'no3', { class4: true, class5: 5, class6: [{ why: '?' }] }) |
| 20 | + // @ts-expect-error `cn` should be stricter and disallow this |
| 21 | + cn('class1', 2) |
| 22 | + // @ts-expect-error `cn` should be stricter and disallow this |
| 23 | + cn('class1', true, 'class3') |
| 24 | + // @ts-expect-error `cn` should be stricter and disallow this |
| 25 | + cn('class1', { class5: 5 }) |
| 26 | + // @ts-expect-error `cn` should be stricter and disallow this |
| 27 | + cn('class1', { class6: [{ why: '?' }] }) |
| 28 | + // Valid |
| 29 | + cn('class1', condition && 'no3', { class4: true }) |
| 30 | +} |
0 commit comments