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

Fix nested types output from createStrictAPI() being unintentionally marked as required #1604

Merged
merged 2 commits into from
Dec 22, 2023

Conversation

itsdouges
Copy link
Collaborator

This pull request updates types and adds some tests for this specific use case.

Copy link

changeset-bot bot commented Dec 22, 2023

🦋 Changeset detected

Latest commit: a99f3f3

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@compiled/babel-plugin Patch
@compiled/react Patch
@compiled/parcel-transformer Patch
@compiled/webpack-loader Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -5,6 +5,58 @@ import type { XCSSProp } from './__fixtures__/strict-api-recursive';
import { css, cssMap } from './__fixtures__/strict-api-recursive';

describe('createStrictAPI()', () => {
it('should mark all styles as optional in css()', () => {
const styles = css({
'&:hover': {},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was a type violation previously.

: TObject[P]
type EnforceSchema<TSchema> = {
[P in keyof TSchema]?: P extends keyof CompiledSchema
? TSchema[P] extends Record<string, any>
Copy link
Collaborator Author

@itsdouges itsdouges Dec 22, 2023

Choose a reason for hiding this comment

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

Fix: unknown -> any

Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting, do you know why this works? I've seen similar things, just never seen like a TS issue or explanation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think it's an edge case with complex nested types (interface + omits + ?). At a glace unknown should be fine but in this case it's obviously not.

I'm not sure!

@@ -98,15 +98,15 @@ export const visitCssMapPath = (
const { sheets, classNames } = transformCssItems(css, meta);
totalSheets.push(...sheets);

if (classNames.length !== 1) {
if (classNames.length > 1) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This allows us to make empty styles now (at least for testing).

Copy link
Collaborator

@kylorhall-atlassian kylorhall-atlassian left a comment

Choose a reason for hiding this comment

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

Seems straightforward from my understanding, and I know there's tests for '&:hover': { … } having valid css in them, so LGTM.

: TObject[P]
type EnforceSchema<TSchema> = {
[P in keyof TSchema]?: P extends keyof CompiledSchema
? TSchema[P] extends Record<string, any>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting, do you know why this works? I've seen similar things, just never seen like a TS issue or explanation.

throw buildCodeFrameError(
createErrorMessage(ErrorMessages.STATIC_VARIANT_OBJECT),
property,
meta.parentPath
);
}

return t.objectProperty(property.key, classNames[0]);
return t.objectProperty(property.key, classNames[0] || t.stringLiteral(''));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Got it, so having '&:hover' empty threw this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah exactly.

@itsdouges itsdouges merged commit 39714ae into master Dec 22, 2023
5 checks passed
@itsdouges itsdouges deleted the fix-nested-strict-type branch December 22, 2023 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants