Automatic Inference
When you define variants, TypeScript infers the types automatically.as const needed. No manual type definitions.
How It Works
better-styled usesconst type parameters to preserve literal types:
const modifier tells TypeScript to infer exact literal types ("sm" | "md" | "lg") instead of widening to string.
Context Type Inference
createStyledContext also uses const type parameters:
["boolean"] array is transformed into a proper boolean type, not "boolean".
Getting Variant Props Type
Sometimes you need the variant props type for other purposes. Use TypeScript’s inference:Shared Configs
When multiple components share the same variants and context, usestyledConfig() to create a single typed config:
styledConfig() uses the same type inference as styled() — no generics needed. TypeScript validates that the config is compatible with both components simultaneously.
styledConfig() is an identity function — it returns the config unchanged. It exists purely for type inference, with zero runtime cost.Extending Components
When you wrap a styled component, types flow through:Strict Variants
By default, variant props are optional. If you want to require them, don’t usedefaultVariants:
Working with Refs
Refs work as expected. The component forwards refs to the underlying element:Generic Components
If you need a component that works with multiple element types:Common Patterns
Omit Specific Variants
Require Children
Tips
Let TypeScript infer
Let TypeScript infer
Don’t add type annotations unless necessary. The inference is designed to work without them.
Use const arrays for context
Use const arrays for context
Always pass arrays directly to
createStyledContext. Don’t assign them to variables first, or you’ll lose the literal types.Check IDE tooltips
Check IDE tooltips
Hover over components in your IDE to see the inferred types. This is the best way to understand what TypeScript sees.
API Reference
Complete API documentation