CSSType - Strict CSS Type Definitions
CSSType provides comprehensive TypeScript and Flow type definitions for CSS properties and values, meticulously generated from MDN data. It is currently at version 3.2.3 and maintains an active release cadence, frequently updating its definitions to reflect the latest CSS specifications and browser compatibility data. Key differentiators include its strict adherence to MDN, support for various property naming conventions (camelCase for JavaScript, kebab-case for CSS-in-JS libraries), and inclusion of fallback array types for properties that accept multiple values. It covers standard, vendor-prefixed, obsolete, and SVG-specific properties, along with at-rule and pseudo-class/element types, offering robust type checking and autocompletion for web development projects.
Common errors
-
Type '"middle"' is not assignable to type '"-moz-initial" | "inherit" | "initial" | "revert" | "revert-layer" | "unset" | ...'.
cause An invalid or unrecognized value was assigned to a CSS property type.fixReplace 'middle' with a valid CSS `text-align` value such as 'center', 'left', 'right', 'start', or 'end'. -
Property 'colour' does not exist on type 'Properties<string | number, string>'. Did you mean 'color'?
cause A non-standard or misspelled CSS property name was used.fixCorrect the property name to 'color', which is the standard CSS property for text color. CSSType follows the standard W3C and MDN property names. -
TS2742: The inferred type of '...' cannot be named without a reference to 'csstype/dist/csstype'. This is likely not portable. A type annotation is necessary.
cause TypeScript might struggle to infer a complex type from `csstype` without an explicit `import type` statement or when using `require()` for types, especially in older TS versions or specific project configurations.fixEnsure you are using `import type * as CSS from 'csstype';` for all `csstype` imports to correctly reference type declarations without bundling runtime values. If the issue persists, explicitly annotate the type, e.g., `const style: CSS.Properties = { ... }`.
Warnings
- breaking CSSType v3 introduced significant breaking changes, primarily impacting how types are structured and accessed. The main `CSS` namespace now aggregates most types, requiring adjustments to existing type imports from previous versions (e.g., `v2`).
- gotcha Incorrect CSS property values will result in TypeScript errors (e.g., `textAlign: 'middle'`). CSSType enforces strict adherence to MDN-defined valid values.
- gotcha Choosing the correct type interface (e.g., `Properties`, `PropertiesHyphen`, `PropertiesFallback`) is crucial for various use cases like CSS-in-JS libraries or style objects that accept array fallbacks.
- gotcha Using custom CSS properties (CSS Variables) with `csstype` might require type assertions or module augmentation, as they are not statically known by the library.
Install
-
npm install csstype -
yarn add csstype -
pnpm add csstype
Imports
- CSS.Properties
import * as CSS from 'csstype';
import type * as CSS from 'csstype';
- CSS.PropertiesHyphen
import { PropertiesHyphen } from 'csstype/dist/prop-types';import type { PropertiesHyphen } from 'csstype'; - CSS.AtRule.FontFace
import type { FontFace } from 'csstype/at-rules';import type { AtRule } from 'csstype';
Quickstart
import type * as CSS from 'csstype';
interface CustomTheme {
primary: CSS.Property.Color;
secondary: CSS.Property.Color;
}
// Example usage with a common CSS-in-JS pattern
const getButtonStyle = (theme: CustomTheme): CSS.Properties => ({
backgroundColor: theme.primary,
color: theme.secondary,
padding: '8px 16px',
borderRadius: '4px',
border: '1px solid currentColor',
':hover': {
opacity: 0.9,
cursor: 'pointer'
},
// Type error: 'middle' is not a valid textAlign value
textAlign: 'middle' as CSS.Property.TextAlign,
// Type error: 'colour' is not a standard CSS property
// @ts-ignore: Intentionally demonstrating a property error
colour: 'red'
});
const myTheme: CustomTheme = {
primary: '#007bff',
secondary: 'white'
};
const buttonStyles = getButtonStyle(myTheme);
console.log(buttonStyles);