Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict mode #116

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 107 additions & 66 deletions packages/css/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -45,14 +93,7 @@ export interface IKeyframesAtom {
[ATOM]: true;
}

export type TRecursiveCss<
T extends TConfig,
D = {
[K in keyof Properties]?: K extends keyof ICssPropToToken<T>
? ICssPropToToken<T>[K] | Properties[K]
: Properties[K];
}
> = (
export type TRecursiveCss<T extends TConfig, D = TFlatCSS<T>> = (
| D
| {
[pseudo: string]: (
Expand All @@ -64,14 +105,13 @@ export type TRecursiveCss<
) &
D;

export type TFlatCSS<
T extends TConfig,
D = {
[K in keyof Properties]?: K extends keyof ICssPropToToken<T>
? ICssPropToToken<T>[K] | Properties[K]
: Properties[K];
}
> = D;
export type TFlatCSS<T extends TConfig> = {
[K in keyof Properties]?: K extends keyof ICssPropToToken<T>
? T extends { strict: true }
? ICssPropToToken<T>[K]
: ICssPropToToken<T>[K] | Properties[K]
: Properties[K];
};

export type TFlatUtils<
T extends TConfig,
Expand Down Expand Up @@ -122,10 +162,10 @@ export type ICssPropToToken<T extends TConfig> = 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"]
Expand All @@ -141,19 +181,19 @@ export type ICssPropToToken<T extends TConfig> = T["tokens"] extends object
keyof T["tokens"]["space"],
keyof T["tokens"]["space"]
]
: never;
: MarginProperty<never>;
marginTop: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: MarginTopProperty<never>;
marginLeft: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: MarginLeftProperty<never>;
marginRight: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: MarginRightProperty<never>;
marginBottom: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: MarginBottomProperty<never>;
padding: T["tokens"]["space"] extends object
?
| keyof T["tokens"]["space"]
Expand All @@ -169,61 +209,61 @@ export type ICssPropToToken<T extends TConfig> = T["tokens"] extends object
keyof T["tokens"]["space"],
keyof T["tokens"]["space"]
]
: never;
: PaddingProperty<never>;
paddingTop: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: PaddingTopProperty<never>;
paddingLeft: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: PaddingLeftProperty<never>;
paddingRight: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: PaddingRightProperty<never>;
paddingBottom: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: PaddingBottomProperty<never>;
gridGap: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: GridGapProperty<never>;
gridColumnGap: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: GridColumnProperty;
gridRowGap: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: GridRowGapProperty<never>;
fontSize: T["tokens"]["fontSizes"] extends object
? keyof T["tokens"]["fontSizes"]
: never;
: FontSizeProperty<never>;
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<never>;
letterSpacing: T["tokens"]["letterSpacings"] extends object
? keyof T["tokens"]["letterSpacings"]
: never;
: LetterSpacingProperty<string>;
flexBasis: T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
: never;
: FlexBasisProperty<string>;
flex:
| (T["tokens"]["space"] extends object
? keyof T["tokens"]["space"]
Expand All @@ -243,67 +283,67 @@ export type ICssPropToToken<T extends TConfig> = T["tokens"] extends object
];
width: T["tokens"]["sizes"] extends object
? keyof T["tokens"]["sizes"]
: never;
: WidthProperty<never>;
height: T["tokens"]["sizes"] extends object
? keyof T["tokens"]["sizes"]
: never;
: HeightProperty<never>;
minWidth: T["tokens"]["sizes"] extends object
? keyof T["tokens"]["sizes"]
: never;
: MinWidthProperty<never>;
maxWidth: T["tokens"]["sizes"] extends object
? keyof T["tokens"]["sizes"]
: never;
: MaxWidthProperty<never>;
minHeight: T["tokens"]["sizes"] extends object
? keyof T["tokens"]["sizes"]
: never;
: MinHeightProperty<never>;
maxHeight: T["tokens"]["sizes"] extends object
? keyof T["tokens"]["sizes"]
: never;
: MaxHeightProperty<never>;
borderWidth: T["tokens"]["borderWidths"] extends object
? keyof T["tokens"]["borderWidths"]
: never;
: BorderWidthProperty<never>;
borderTopWidth: T["tokens"]["borderWidths"] extends object
? keyof T["tokens"]["borderWidths"]
: never;
: BorderTopWidthProperty<string>;
borderLeftWidth: T["tokens"]["borderWidths"] extends object
? keyof T["tokens"]["borderWidths"]
: never;
: BorderLeftWidthProperty<string>;
borderRightWidth: T["tokens"]["borderWidths"] extends object
? keyof T["tokens"]["borderWidths"]
: never;
: BorderRightWidthProperty<string>;
borderBottomWidth: T["tokens"]["borderWidths"] extends object
? keyof T["tokens"]["borderWidths"]
: never;
: BorderBottomWidthProperty<string>;
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<never>;
borderTopLeftRadius: T["tokens"]["radii"] extends object
? keyof T["tokens"]["radii"]
: never;
: BorderTopLeftRadiusProperty<never>;
borderTopRightRadius: T["tokens"]["radii"] extends object
? keyof T["tokens"]["radii"]
: never;
: BorderTopRightRadiusProperty<never>;
borderBottomRightRadius: T["tokens"]["radii"] extends object
? keyof T["tokens"]["radii"]
: never;
: BorderBottomRightRadiusProperty<never>;
borderBottomLeftRadius: T["tokens"]["radii"] extends object
? keyof T["tokens"]["radii"]
: never;
: BorderBottomLeftRadiusProperty<never>;
boxShadow: T["tokens"]["shadows"] extends object
? keyof T["tokens"]["shadows"]
: [
Expand All @@ -316,13 +356,13 @@ export type ICssPropToToken<T extends TConfig> = 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;
}
: {};

Expand Down Expand Up @@ -355,6 +395,7 @@ export type TConfig<STRICT_MODE extends boolean = false> = {
showFriendlyClassnames?: boolean;
prefix?: string;
utilityFirst?: boolean;
strict?: boolean;
} & (STRICT_MODE extends true
? { breakpoints: IBreakpoints; tokens: ITokensDefinition; utils: IUtils }
: {
Expand Down
44 changes: 44 additions & 0 deletions packages/css/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw in a diff PR you used @ts-expect-error. just wondering if its also worth using it here instead of a @ts-ignore?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had @ts-expect-error here first but for some reason the tests then failed, even though the build succeeded. I didn't investigate further why things are different when running through ts-jest and opted for this quick fix.

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