I am extending the styled-components module like
import "styled-components";
declare module "styled-components" {
type Breakpoint = "small" | "medium" | "large" | "extra";
export interface DefaultTheme {
devices: { [P in keyof Breakpoint]: number };
colors: {
main: string;
secondary: string;
};
}
}
I use it here
import { DefaultTheme } from "styled-components";
const breakpoints = {
small: 576,
medium: 768,
large: 992,
extra: 1200
};
const theme: DefaultTheme = {
devices: Object.fromEntries<string>(
Object.entries(breakpoints).map(([k, v]) => [k, `(min-width: ${v}px)`])
),
colors: {
main: "blue",
secondary: "red"
}
};
export default theme;
How can I get proper type safety here? 2 issues I'm having:
theme.devices
:
Type '{ [x: string]: string; [x: number]: string; }' is missing the following properties from type '{ [x: number]: number; toString: number; charAt: number; charCodeAt: number; concat: number; indexOf: number; lastIndexOf: number; localeCompare: number; match: number; replace: number; search: number; slice: number; ... 34 more ...; trimRight: number; }': charAt, charCodeAt, concat, indexOf, and 40 more.
And where I try to use it I get all string methods along with the custom breakpoints properties while I would only like to have the breakpoints properties
`@media ${props => props.theme.devices.small}` {