Skip to content

Instantly share code, notes, and snippets.

@freddi301
Last active May 12, 2023 09:34
Show Gist options
  • Save freddi301/4b10e8977b404b852052daea8f310770 to your computer and use it in GitHub Desktop.
Save freddi301/4b10e8977b404b852052daea8f310770 to your computer and use it in GitHub Desktop.
typescript style-components lite
import type { CSSProp } from "styled-components/index";
declare module "react" {
interface Attributes {
css?: CSSProp;
css?: any // if compilation times becomes too high
}
}
// import "styled-components/macro" must be imported in every file that uses css={`color: red`}
// slimmer type definition than @types/styled-components
// this compiles faster
declare module "styled-components" {
export const css: StyledCSSFunction;
const styled: Styled;
export = styled;
}
declare module "styled-components/macro" {
export const css: StyledCSSFunction;
const styled: Styled;
export = styled;
}
type StyledComponentFactory = {
[E in "div" | "button" | "input" | "header" | "p" | "dialog" | "span" | "img" | "form" | "b" | "small" | "label"]: <
Props = {}
>(
string: TemplateStringsArray,
...interpolations: Interpolations<Props>
) => React.FunctionComponent<JSX.IntrinsicElements[E] & Props & { theme?: any }> & { [isStyled]: true };
};
interface Styled extends StyledComponentFactory {
<Props>(styledComponent: React.FunctionComponent<Props>): (
string: TemplateStringsArray,
...interpolations: Interpolations<Props>
) => React.FunctionComponent<Props> & { [isStyled]: true };
}
type Interpolations<Props> = Array<
string | number | ((props: Props & { theme?: any }) => string | number) | { [isStyled]: true }
>;
const isStyled = Symbol();
type StyledCSSFunction = (string: TemplateStringsArray, ...interpolations: Array<string | number>) => any;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment