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(types): restrict regular styles function call to one argument #38

Merged
merged 1 commit into from
Oct 10, 2022
Merged

fix(types): restrict regular styles function call to one argument #38

merged 1 commit into from
Oct 10, 2022

Conversation

100terres
Copy link
Contributor

@100terres 100terres commented Oct 3, 2022

What's included

fix: #37
fix: #15

Tag functions are properly typed and regular function calls are restrict to one argument (see: #38 (comment))

  • keyframes
  • glob
  • css
  • createGlobalStyles
  • The return of the styled(...)

We can now override the types to mark the theme as always available on the props. (see: #38 (comment))

@100terres 100terres changed the title fix(types): regular styles function call should be restrcited to one argument fix(types): restrict regular styles function call to one argument Oct 3, 2022
Comment on lines +54 to +56
export interface ThemeProp {
theme?: DefaultTheme;
}
Copy link
Contributor Author

@100terres 100terres Oct 6, 2022

Choose a reason for hiding this comment

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

This is would allow us to override the props in a project with a theme with something like this:

// in a styled.d.ts file
declare module "solid-styled-components" {
  export interface DefaultTheme {
    // ...
  }

  // The theme will always be present
  export interface ThemeProp {
    theme: DefaultTheme;
    //   ^
    // The `?` was removed 
  }

We won't need to make sure that the theme exist on the props e.g.

const Button = styled("button")((props) => ({
  color: props.theme.typography.color,
  //          ^
  // we won't need to check if the theme is present or not
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could extract this in an other PR if needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not possible with the current version of TypeScript. I've added a comment on an existing issue on the TypeScript repo: microsoft/TypeScript#33699 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now using the non-null assertion operator should do the trick:

styled("span")`
  color: ${(props) => props.theme!.palette}
`;

Comment on lines +35 to 39
export declare function css(styles: StylesArg): string;
export declare function css(
tag: CSSAttribute | TemplateStringsArray | string,
...props: Array<string | number>
tag: TemplateStringsArray,
...tagArgs: TagArgs
): string;
Copy link
Contributor Author

@100terres 100terres Oct 6, 2022

Choose a reason for hiding this comment

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

By having multiple declarations we can restrict the use of multiple arguments to the "tag function" and make sure that regular function call are restircted to one argument.

// allowed
css`
  color: ${theme.typography.color};
`
// allowed
css({
  color: ${theme.typography.color},
});

// With the current implementation the following is allow
// but with this PR the following would be disallowed
css(
  {
    color: theme.typography.color,
  },
  // When we are not using a tag function,
  // only the first argument is used by goober
  // The following won't be added to goober
  {
    font-family: theme.typography.fontFamily,
  },
);

@100terres 100terres marked this pull request as ready for review October 7, 2022 01:57
@ryansolid ryansolid merged commit 5860b3b into solidjs:main Oct 10, 2022
@ryansolid
Copy link
Member

Thank you

Comment on lines +83 to +85
...tagArgs: TagArgs
): () => null;
export declare function createGlobalStyles(styles: StylesArg): () => null;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the theme is available in the createGlobalStyles. I'll have a look and see if it can be added.

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.

Inconsistency between styled's implementation and types styled: multiple arguments not working(?)
2 participants