Gathering detailed insights and metrics for csstype
Gathering detailed insights and metrics for csstype
Gathering detailed insights and metrics for csstype
Gathering detailed insights and metrics for csstype
Strict TypeScript and Flow types for style based on MDN data
npm install csstype
99.2
Supply Chain
99.6
Quality
79.2
Maintenance
100
Vulnerability
100
License
Module System
Unable to determine the module system for this package.
Min. Node Version
Typescript Support
Node Version
NPM Version
1,739 Stars
359 Commits
74 Forks
10 Watching
8 Branches
12 Contributors
Updated on 20 Nov 2024
TypeScript (98%)
JavaScript (2%)
Cumulative downloads
Total Downloads
Last day
-6.5%
5,755,811
Compared to previous day
Last week
2.5%
32,961,460
Compared to previous week
Last month
7.6%
137,813,047
Compared to previous month
Last year
8%
1,486,810,665
Compared to previous year
28
TypeScript and Flow definitions for CSS, generated by data from MDN. It provides autocompletion and type checking for CSS properties and values.
TypeScript
1import type * as CSS from 'csstype'; 2 3const style: CSS.Properties = { 4 colour: 'white', // Type error on property 5 textAlign: 'middle', // Type error on value 6};
Flow
1// @flow strict 2import * as CSS from 'csstype'; 3 4const style: CSS.Properties<> = { 5 colour: 'white', // Type error on property 6 textAlign: 'middle', // Type error on value 7};
Further examples below will be in TypeScript!
1$ npm install csstype
Properties are categorized in different uses and in several technical variations to provide typings that suits as many as possible.
Default | Hyphen | Fallback | HyphenFallback | |
---|---|---|---|---|
All | Properties | PropertiesHyphen | PropertiesFallback | PropertiesHyphenFallback |
Standard | StandardProperties | StandardPropertiesHyphen | StandardPropertiesFallback | StandardPropertiesHyphenFallback |
Vendor | VendorProperties | VendorPropertiesHyphen | VendorPropertiesFallback | VendorPropertiesHyphenFallback |
Obsolete | ObsoleteProperties | ObsoletePropertiesHyphen | ObsoletePropertiesFallback | ObsoletePropertiesHyphenFallback |
Svg | SvgProperties | SvgPropertiesHyphen | SvgPropertiesFallback | SvgPropertiesHyphenFallback |
Categories:
Standard
, Vendor
, Obsolete
and Svg
Standard
- Current properties and extends subcategories StandardLonghand
and StandardShorthand
(e.g. StandardShorthandProperties
)Vendor
- Vendor prefixed properties and extends subcategories VendorLonghand
and VendorShorthand
(e.g. VendorShorthandProperties
)Obsolete
- Removed or deprecated propertiesSvg
- SVG-specific propertiesVariations:
Hyphen
- CSS (kebab) cased property namesFallback
- Also accepts array of values e.g. string | string[]
At-rule interfaces with descriptors.
TypeScript: These will be found in the AtRule
namespace, e.g. AtRule.Viewport
.
Flow: These will be prefixed with AtRule$
, e.g. AtRule$Viewport
.
Default | Hyphen | Fallback | HyphenFallback | |
---|---|---|---|---|
@counter-style | CounterStyle | CounterStyleHyphen | CounterStyleFallback | CounterStyleHyphenFallback |
@font-face | FontFace | FontFaceHyphen | FontFaceFallback | FontFaceHyphenFallback |
@viewport | Viewport | ViewportHyphen | ViewportFallback | ViewportHyphenFallback |
String literals of pseudo classes and pseudo elements
Pseudos
Extends:
AdvancedPseudos
Function-like pseudos e.g. :not(:first-child)
. The string literal contains the value excluding the parenthesis: :not
. These are separated because they require an argument that results in infinite number of variations.
SimplePseudos
Plain pseudos e.g. :hover
that can only be one variation.
All interfaces has two optional generic argument to define length and time: CSS.Properties<TLength = string | 0, TTime = string>
string | 0
because 0
is the only length where the unit identifier is optional. You can specify this, e.g. string | number
, for platforms and libraries that accepts any numeric value as length with a specific unit.
1const style: CSS.Properties<string | number> = { 2 width: 100, 3};
string
. You can specify this, e.g. string | number
, for platforms and libraries that accepts any numeric value as length with a specific unit.
1const style: CSS.Properties<string | number, number> = { 2 transitionDuration: 1000, 3};
1import type * as CSS from 'csstype'; 2 3const style: CSS.Properties = { 4 width: '10px', 5 margin: '1em', 6};
In some cases, like for CSS-in-JS libraries, an array of values is a way to provide fallback values in CSS. Using CSS.PropertiesFallback
instead of CSS.Properties
will add the possibility to use any property value as an array of values.
1import type * as CSS from 'csstype'; 2 3const style: CSS.PropertiesFallback = { 4 display: ['-webkit-flex', 'flex'], 5 color: 'white', 6};
There's even string literals for pseudo selectors and elements.
1import type * as CSS from 'csstype'; 2 3const pseudos: { [P in CSS.SimplePseudos]?: CSS.Properties } = { 4 ':hover': { 5 display: 'flex', 6 }, 7};
Hyphen cased (kebab cased) properties are provided in CSS.PropertiesHyphen
and CSS.PropertiesHyphenFallback
. It's not not added by default in CSS.Properties
. To allow both of them, you can simply extend with CSS.PropertiesHyphen
or/and CSS.PropertiesHyphenFallback
.
1import type * as CSS from 'csstype'; 2 3interface Style extends CSS.Properties, CSS.PropertiesHyphen {} 4 5const style: Style = { 6 'flex-grow': 1, 7 'flex-shrink': 0, 8 'font-weight': 'normal', 9 backgroundColor: 'white', 10};
Adding type checked CSS properties to a HTMLElement
.
1import type * as CSS from 'csstype'; 2 3const style: CSS.Properties = { 4 color: 'red', 5 margin: '1em', 6}; 7 8let button = document.createElement('button'); 9 10Object.assign(button.style, style);
The goal is to have as perfect types as possible and we're trying to do our best. But with CSS Custom Properties, the CSS specification changing frequently and vendors implementing their own specifications with new releases sometimes causes type errors even if it should work. Here's some steps you could take to get it fixed:
If you're using CSS Custom Properties you can step directly to step 3.
First of all, make sure you're doing it right. A type error could also indicate that you're not :wink:
Type 'string' is not assignable to...
errorsHave a look in issues to see if an issue already has been filed. If not, create a new one. To help us out, please refer to any information you have found.
Fix the issue locally with TypeScript (Flow further down):
The recommended way is to use module augmentation. Here's a few examples:
1// My css.d.ts file 2import type * as CSS from 'csstype'; 3 4declare module 'csstype' { 5 interface Properties { 6 // Add a missing property 7 WebkitRocketLauncher?: string; 8 9 // Add a CSS Custom Property 10 '--theme-color'?: 'black' | 'white'; 11 12 // Allow namespaced CSS Custom Properties 13 [index: `--theme-${string}`]: any; 14 15 // Allow any CSS Custom Properties 16 [index: `--${string}`]: any; 17 18 // ...or allow any other property 19 [index: string]: any; 20 } 21}
The alternative way is to use type assertion. Here's a few examples:
1const style: CSS.Properties = { 2 // Add a missing property 3 ['WebkitRocketLauncher' as any]: 'launching', 4 5 // Add a CSS Custom Property 6 ['--theme-color' as any]: 'black', 7};
Fix the issue locally with Flow:
Use type assertion. Here's a few examples:
1const style: $Exact<CSS.Properties<*>> = { 2 // Add a missing property 3 [('WebkitRocketLauncher': any)]: 'launching', 4 5 // Add a CSS Custom Property 6 [('--theme-color': any)]: 'black', 7};
Property.AlignContent
(was AlignContentProperty
before)Property$AlignContent
AtRule.FontFace
(was FontFace
before)AtRule$FontFace
Color
and Box
. Because the generation of data types may suddenly be removed or renamed.(string & {})
for literal string unions and (number & {})
for literal number unions (related issue). Utilize PropertyValue<T>
to unpack types from e.g. (string & {})
to string
.Never modify index.d.ts
and index.js.flow
directly. They are generated automatically and committed so that we can easily follow any change it results in. Therefor it's important that you run $ git config merge.ours.driver true
after you've forked and cloned. That setting prevents merge conflicts when doing rebase.
npm run build
Generates typings and type checks themnpm run watch
Runs build on each savenpm run test
Runs the testsnpm run lazy
Type checks, lints and formats everythingNo vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 3/25 approved changesets -- score normalized to 1
Reason
9 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More