使用TypeScript实现CSS断点混合时出现问题



基于https://tobbelindstrom.com/blog/how-to-create-a-breakpoint-mixin-with-styled-components/我试图在TypeScript中做同样的事情,但我没能键入这个函数。

import { css } from 'styled-components';
import { breakpoints } from './_variables';
export const respondTo = Object.keys(breakpoints).reduce(
(accumulator, label) => {
accumulator[label] = (...args) => css`
@media (min-width: ${breakpoints[label]}) {
${css(...args)};
}
`;
return accumulator;
},
{}
);

我试过这样的东西:

export const respondTo = Object.keys(breakpoints).reduce<Record<string, Function>>(
(accumulator, label) => {
accumulator[label] = (...args: Array<String>) => css`
@media (min-width: ${breakpoints[label]}) {
${css(...args)};
}
`;
return accumulator;
},
{}
);

但它不断抛出错误。现在,它给了我

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.   No index signature with a parameter of type 'string' was found on type '{}'.  TS7053

在CCD_ 1处。

这是工作

import { css } from "styled-components";
const breakpoints = {
xs: "480px",
sm: "768px",
md: "992px",
lg: "1200px"
};
type cssParams = Parameters<typeof css>;
const keys = Object.keys(breakpoints) as Array<keyof typeof breakpoints>;
export const respondTo = keys.reduce((accumulator, label) => {
accumulator[label] = (...args: cssParams) => {
return css`
@media (min-width: ${breakpoints[label]}) {
${css(...args)};
}
`;
};
return accumulator;
}, {} as Record<keyof typeof breakpoints, Function>);

问题是breakpoints[label]Object.keys()返回类型string[],因此label变为string,这不是breakpoints的有效密钥。

或者,您可以将breakpoints重新键入类似Record<string, string>的内容。

我还添加了breakpoints[label]0,它将在解决此问题后为您提供帮助;(

最新更新