diff --git a/packages/css/src/types.ts b/packages/css/src/types.ts index 6cc6e683..b0da6eae 100644 --- a/packages/css/src/types.ts +++ b/packages/css/src/types.ts @@ -4,6 +4,54 @@ import { LineStyle, LineWidth, Properties, + MarginProperty, + MarginTopProperty, + MarginLeftProperty, + MarginRightProperty, + MarginBottomProperty, + PaddingProperty, + PaddingTopProperty, + PaddingLeftProperty, + PaddingRightProperty, + PaddingBottomProperty, + GridGapProperty, + GridColumnProperty, + GridRowGapProperty, + FontSizeProperty, + BorderColorProperty, + BorderTopColorProperty, + BorderLeftColorProperty, + BorderRightColorProperty, + BorderBottomColorProperty, + FontFamilyProperty, + FontWeightProperty, + LineHeightProperty, + LetterSpacingProperty, + FlexBasisProperty, + WidthProperty, + HeightProperty, + MinWidthProperty, + MaxWidthProperty, + MinHeightProperty, + MaxHeightProperty, + BorderWidthProperty, + BorderTopWidthProperty, + BorderLeftWidthProperty, + BorderRightWidthProperty, + BorderBottomWidthProperty, + BorderStyleProperty, + BorderTopStyleProperty, + BorderLeftStyleProperty, + BorderRightStyleProperty, + BorderBottomStyleProperty, + BorderRadiusProperty, + BorderTopLeftRadiusProperty, + BorderTopRightRadiusProperty, + BorderBottomRightRadiusProperty, + BorderBottomLeftRadiusProperty, + TextShadowProperty, + ZIndexProperty, + TransitionProperty, } from "./css-types"; export interface IBreakpoints { @@ -45,14 +93,7 @@ export interface IKeyframesAtom { [ATOM]: true; } -export type TRecursiveCss< - T extends TConfig, - D = { - [K in keyof Properties]?: K extends keyof ICssPropToToken - ? ICssPropToToken[K] | Properties[K] - : Properties[K]; - } -> = ( +export type TRecursiveCss> = ( | D | { [pseudo: string]: ( @@ -64,14 +105,13 @@ export type TRecursiveCss< ) & D; -export type TFlatCSS< - T extends TConfig, - D = { - [K in keyof Properties]?: K extends keyof ICssPropToToken - ? ICssPropToToken[K] | Properties[K] - : Properties[K]; - } -> = D; +export type TFlatCSS = { + [K in keyof Properties]?: K extends keyof ICssPropToToken + ? T extends { strict: true } + ? ICssPropToToken[K] + : ICssPropToToken[K] | Properties[K] + : Properties[K]; +}; export type TFlatUtils< T extends TConfig, @@ -122,10 +162,10 @@ export type ICssPropToToken = T["tokens"] extends object ]; color: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : Color; backgroundColor: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : Color; margin: T["tokens"]["space"] extends object ? | keyof T["tokens"]["space"] @@ -141,19 +181,19 @@ export type ICssPropToToken = T["tokens"] extends object keyof T["tokens"]["space"], keyof T["tokens"]["space"] ] - : never; + : MarginProperty; marginTop: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : MarginTopProperty; marginLeft: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : MarginLeftProperty; marginRight: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : MarginRightProperty; marginBottom: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : MarginBottomProperty; padding: T["tokens"]["space"] extends object ? | keyof T["tokens"]["space"] @@ -169,61 +209,61 @@ export type ICssPropToToken = T["tokens"] extends object keyof T["tokens"]["space"], keyof T["tokens"]["space"] ] - : never; + : PaddingProperty; paddingTop: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : PaddingTopProperty; paddingLeft: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : PaddingLeftProperty; paddingRight: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : PaddingRightProperty; paddingBottom: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : PaddingBottomProperty; gridGap: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : GridGapProperty; gridColumnGap: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : GridColumnProperty; gridRowGap: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : GridRowGapProperty; fontSize: T["tokens"]["fontSizes"] extends object ? keyof T["tokens"]["fontSizes"] - : never; + : FontSizeProperty; borderColor: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : BorderColorProperty; borderTopColor: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : BorderTopColorProperty; borderLeftColor: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : BorderLeftColorProperty; borderRightColor: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : BorderRightColorProperty; borderBottomColor: T["tokens"]["colors"] extends object ? keyof T["tokens"]["colors"] - : never; + : BorderBottomColorProperty; fontFamily: T["tokens"]["fonts"] extends object ? keyof T["tokens"]["fonts"] - : never; + : FontFamilyProperty; fontWeight: T["tokens"]["fontWeights"] extends object ? keyof T["tokens"]["fontWeights"] - : never; + : FontWeightProperty; lineHeight: T["tokens"]["lineHeights"] extends object ? keyof T["tokens"]["lineHeights"] - : never; + : LineHeightProperty; letterSpacing: T["tokens"]["letterSpacings"] extends object ? keyof T["tokens"]["letterSpacings"] - : never; + : LetterSpacingProperty; flexBasis: T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] - : never; + : FlexBasisProperty; flex: | (T["tokens"]["space"] extends object ? keyof T["tokens"]["space"] @@ -243,67 +283,67 @@ export type ICssPropToToken = T["tokens"] extends object ]; width: T["tokens"]["sizes"] extends object ? keyof T["tokens"]["sizes"] - : never; + : WidthProperty; height: T["tokens"]["sizes"] extends object ? keyof T["tokens"]["sizes"] - : never; + : HeightProperty; minWidth: T["tokens"]["sizes"] extends object ? keyof T["tokens"]["sizes"] - : never; + : MinWidthProperty; maxWidth: T["tokens"]["sizes"] extends object ? keyof T["tokens"]["sizes"] - : never; + : MaxWidthProperty; minHeight: T["tokens"]["sizes"] extends object ? keyof T["tokens"]["sizes"] - : never; + : MinHeightProperty; maxHeight: T["tokens"]["sizes"] extends object ? keyof T["tokens"]["sizes"] - : never; + : MaxHeightProperty; borderWidth: T["tokens"]["borderWidths"] extends object ? keyof T["tokens"]["borderWidths"] - : never; + : BorderWidthProperty; borderTopWidth: T["tokens"]["borderWidths"] extends object ? keyof T["tokens"]["borderWidths"] - : never; + : BorderTopWidthProperty; borderLeftWidth: T["tokens"]["borderWidths"] extends object ? keyof T["tokens"]["borderWidths"] - : never; + : BorderLeftWidthProperty; borderRightWidth: T["tokens"]["borderWidths"] extends object ? keyof T["tokens"]["borderWidths"] - : never; + : BorderRightWidthProperty; borderBottomWidth: T["tokens"]["borderWidths"] extends object ? keyof T["tokens"]["borderWidths"] - : never; + : BorderBottomWidthProperty; borderStyle: T["tokens"]["borderStyles"] extends object ? keyof T["tokens"]["borderStyles"] - : never; + : BorderStyleProperty; borderTopStyle: T["tokens"]["borderStyles"] extends object ? keyof T["tokens"]["borderStyles"] - : never; + : BorderTopStyleProperty; borderLeftStyle: T["tokens"]["borderStyles"] extends object ? keyof T["tokens"]["borderStyles"] - : never; + : BorderLeftStyleProperty; borderRightStyle: T["tokens"]["borderStyles"] extends object ? keyof T["tokens"]["borderStyles"] - : never; + : BorderRightStyleProperty; borderBottomStyle: T["tokens"]["borderStyles"] extends object ? keyof T["tokens"]["borderStyles"] - : never; + : BorderBottomStyleProperty; borderRadius: T["tokens"]["radii"] extends object ? keyof T["tokens"]["radii"] - : never; + : BorderRadiusProperty; borderTopLeftRadius: T["tokens"]["radii"] extends object ? keyof T["tokens"]["radii"] - : never; + : BorderTopLeftRadiusProperty; borderTopRightRadius: T["tokens"]["radii"] extends object ? keyof T["tokens"]["radii"] - : never; + : BorderTopRightRadiusProperty; borderBottomRightRadius: T["tokens"]["radii"] extends object ? keyof T["tokens"]["radii"] - : never; + : BorderBottomRightRadiusProperty; borderBottomLeftRadius: T["tokens"]["radii"] extends object ? keyof T["tokens"]["radii"] - : never; + : BorderBottomLeftRadiusProperty; boxShadow: T["tokens"]["shadows"] extends object ? keyof T["tokens"]["shadows"] : [ @@ -316,13 +356,13 @@ export type ICssPropToToken = T["tokens"] extends object ]; textShadow: T["tokens"]["shadows"] extends object ? keyof T["tokens"]["shadows"] - : never; + : TextShadowProperty; zIndex: T["tokens"]["zIndices"] extends object ? keyof T["tokens"]["zIndices"] - : never; + : ZIndexProperty; transition: T["tokens"]["transitions"] extends object ? keyof T["tokens"]["transitions"] - : never; + : TransitionProperty; } : {}; @@ -355,6 +395,7 @@ export type TConfig = { showFriendlyClassnames?: boolean; prefix?: string; utilityFirst?: boolean; + strict?: boolean; } & (STRICT_MODE extends true ? { breakpoints: IBreakpoints; tokens: ITokensDefinition; utils: IUtils } : { diff --git a/packages/css/tests/index.test.ts b/packages/css/tests/index.test.ts index 983384ef..54c2a33e 100644 --- a/packages/css/tests/index.test.ts +++ b/packages/css/tests/index.test.ts @@ -995,4 +995,48 @@ describe("createCss", () => { ._bZKhEt{font-size:1.2em;}" `); }); + + test("should not yield type errors when raw values are used even though tokens are defined", () => { + const css = createCss({ + tokens: { + colors: { + primary: "red", + }, + }, + }); + css({ + color: "lime", + }); + }); + + test("should yield type errors in strict mode when tokens are defined but raw values are used", () => { + const css = createCss({ + strict: true, + tokens: { + colors: { + primary: "red", + }, + }, + }); + css({ + color: "primary", + // @ts-ignore + backgroundColor: "red", + }); + }); + + test("should not yield type errors when a token category is missing", () => { + const css = createCss({ + strict: true, + tokens: { + radii: { + tiny: "3px", + }, + }, + }); + css({ + borderRadius: "tiny", + color: "lime", // allowed since no color tokens are defined + }); + }); });