-
Notifications
You must be signed in to change notification settings - Fork 27
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
fix(types): restrict regular styles function call to one argument #38
Conversation
export interface ThemeProp { | ||
theme?: DefaultTheme; | ||
} |
There was a problem hiding this comment.
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
);
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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}
`;
export declare function css(styles: StylesArg): string; | ||
export declare function css( | ||
tag: CSSAttribute | TemplateStringsArray | string, | ||
...props: Array<string | number> | ||
tag: TemplateStringsArray, | ||
...tagArgs: TagArgs | ||
): string; |
There was a problem hiding this comment.
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,
},
);
Thank you |
...tagArgs: TagArgs | ||
): () => null; | ||
export declare function createGlobalStyles(styles: StylesArg): () => null; |
There was a problem hiding this comment.
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.
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
styled(...)
We can now override the types to mark the
theme
as always available on the props. (see: #38 (comment))