TypeScript Support
Styled-static is built with TypeScript and provides complete type safety for all APIs, with full inference for component props and variants.Type Inference
Styled Components
Component types are automatically inferred from the element or component being styled:Extended Components
When extending components, props are preserved:Custom Components
Extending custom components with typed props:Variant Types
Typed Variants
Variant props are automatically inferred from the configuration:Extracting Variant Props
UseVariantProps to extract variant prop types:
CSS Variants Type
Advanced Types
PropsOf Utility
Extract props from HTML tags or components:StyledComponent Type
Type for styled components with explicit typing:Keyframes Type
Typed keyframe animations:Type-Safe Attrs
Attributes are type-checked based on the element:Dynamic Attrs
Attrs can be functions that receive props:Polymorphism Types
Type-safe polymorphic components withwithComponent:
Generic Components
Create generic styled components:Type Safety Best Practices
1. Use as const for Variant Configs
Preserve literal types for better inference:
2. Export Component Types
Make component types available to consumers:3. Type Component Wrappers
When wrapping styled components, preserve types:4. Strict Mode Configuration
Enable strict TypeScript checks:Common Type Patterns
Forwarding Refs
React 19+ automatically forwards refs, noforwardRef needed:
Discriminated Unions with Variants
Combine variants with discriminated union types:Extending Native Elements
Add custom props to native elements:IDE Support
VSCode
Styled-static works with VSCode’s built-in TypeScript support. For better CSS syntax highlighting in template literals, install: vscode-styled-components This provides:- CSS syntax highlighting
- IntelliSense for CSS properties
- Autocomplete for CSS values
Other IDEs
Most modern IDEs (WebStorm, IntelliJ IDEA) have built-in support for TypeScript and template literal syntax highlighting.Troubleshooting Types
Type Errors in Styled Components
Issue: Component types not inferred correctly Solution: Explicitly type the component:Variant Type Errors
Issue: Variant props not recognized Solution: Useas const for variant config:
Generic Component Types
Issue: Generic components lose type information Solution: UseComponentType with explicit generics:
Related
- Configuration - Plugin configuration
- Library Builds - TypeScript in libraries
- styled API - Complete API types