CSSType - Strict CSS Type Definitions

3.2.3 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates importing `csstype` types, defining an interface using CSS property types, and applying them within a styled object, highlighting type checking for property names and values.

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);

view raw JSON →