Isolated declarations
Export components and recipes with variants without bloating your .d.ts files.
An exported cva or sva recipe copies its CSS into your .d.ts:
export const button = cva({
variants: {
visual: {
solid: { bg: 'blue.500', color: 'white' },
outline: { borderWidth: '1px' }
}
}
})// emitted .d.ts
export declare const button: CvaRuntimeFn<{
visual: {
solid: { bg: 'blue.500'; color: 'white' }
outline: { borderWidth: '1px' }
}
}>The inferred type has no name, so TypeScript writes out the whole structure. With
isolatedDeclarations (opens in a new tab) on, the build fails instead:
every exported const set from a function call needs an explicit type.
The fix
Annotate the export with the variant keys only:
import { cva } from 'styled-system/css'
import type { RecipeRuntimeFn } from 'styled-system/types'
export const button: RecipeRuntimeFn<{ visual?: 'solid' | 'outline' }> = cva({
variants: {
visual: {
solid: { bg: 'blue.500', color: 'white' },
outline: { borderWidth: '1px' }
}
}
})The .d.ts becomes RecipeRuntimeFn<{ visual?: 'solid' | 'outline' }>. The recipe works the same.
In a package built on a design system, import these types from its types export, for example @acme/ds/types.
panda lib adds that export for you.
sva
Use SlotRecipeRuntimeFn<Slots, Props>:
import type { SlotRecipeRuntimeFn } from 'styled-system/types'
export const card: SlotRecipeRuntimeFn<'root' | 'icon', { visual?: 'solid' | 'outline' }> = sva({
slots: ['root', 'icon'],
variants: {
visual: {
solid: { root: { bg: 'blue.500' } },
outline: { root: { borderWidth: '1px' } }
}
}
})styled
styled() already emits a clean type, so you only need an annotation under isolatedDeclarations. Use
StyledComponent<Tag, Props>:
import { styled, type StyledComponent } from 'styled-system/jsx'
export const Button: StyledComponent<'button', { visual?: 'solid' | 'outline' }> = styled('button', {
variants: {
visual: {
solid: { bg: 'blue.500', color: 'white' },
outline: { borderWidth: '1px' }
}
}
})Boolean variants
A { true: {...}, false: {...} } variant is typed as boolean, so annotate it as disabled?: boolean, not
'true' | 'false'.
Skip the annotation
Move the recipe into panda.config.ts and Panda generates a named type for it:
import { button, type ButtonVariantProps } from 'styled-system/recipes'
export const Button = (props: ButtonVariantProps) => {
const [variants, rest] = button.splitVariantProps(props)
return <button className={button(variants)} {...rest} />
}Config recipes are also the way out when an annotation drifts. You write the variant keys by hand, so nothing reminds you to update them, and variants built from spreads or computed keys can't be annotated at all.