You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Styled Components模板字符串引发StyleLint失效问题求助

问题:StyleLint在Styled Components中使用主题字体变量时失效

在以下Styled Components代码中,StyleLint无法正常运行,注释掉${({ theme }) => theme.fonts.body2};这一行后,StyleLint即可恢复正常工作。

原代码

const ModalMent = styled.span`
  color: ${({ theme }) => theme.colors.white};
  ${({ theme }) => theme.fonts.body2};
  width: 14.4rem;
  text-align: center;
  margin-bottom: 0.8rem;
  margin-top: 2.4rem;
`;

注释字体变量后可正常lint的代码

const ModalMent = styled.span`
  margin-top: 2.4rem;
  margin-bottom: 0.8rem;
  /* ${({ theme }) => theme.fonts.body2}; */
  width: 14.4rem;
  text-align: center;
  color: ${({ theme }) => theme.colors.white};
`;

相关代码片段

主题文件(theme.ts)

const FONT = ({ family, weight, size, lineHeight }: Font): RuleSet => {
  return css`
    line-height: ${lineHeight}rem;
    font-family: ${family};
    font-size: ${size}rem;
    font-weight: ${weight};
  `;
};

const fonts = {
  head1: FONT({ family: 'Pretendard Variable', weight: 600, size: 2.8, lineHeight: 3.4 }),
  head2: FONT({ family: 'Pretendard Variable', weight: 700, size: 2.2, lineHeight: 3 }),

  title1: FONT({ family: 'Pretendard Variable', weight: 600, size: 1.8, lineHeight: 2.4 }),
  title2: FONT({ family: 'Pretendard Variable', weight: 400, size: 1.6, lineHeight: 2 }),

  body1: FONT({ family: 'Pretendard Variable', weight: 500, size: 1.6, lineHeight: 2.4 }),
  body2: FONT({ family: 'Pretendard Variable', weight: 500, size: 1.4, lineHeight: 2 }),
  body3: FONT({ family: 'Pretendard Variable', weight: 400, size: 1.4, lineHeight: 2 }),
  body4: FONT({ family: 'Pretendard Variable', weight: 500, size: 1.2, lineHeight: 1.6 }),

  button1: FONT({ family: 'Pretendard Variable', weight: 500, size: 1.6, lineHeight: 2 }),
  button2: FONT({ family: 'Pretendard Variable', weight: 400, size: 1.6, lineHeight: 2 }),
};

StyleLint配置文件(.stylelintrc)

{
  "processors": ["stylelint-processor-styled-components"],
  "extends": [
    "stylelint-config-recommended",
    "stylelint-config-styled-components",
    "stylelint-config-prettier",
    "stylelint-config-concentric-order"
  ],
  "overrides": [
    {
      "files": ["**/*.{ts,tsx}"],
      "customSyntax": "@stylelint/postcss-css-in-js"
    }
  ],
  "rules": {
    "color-named": "never"
  }
}

我推测问题出在theme.fonts.body2的返回类型上,导致StyleLint无法正确识别,但不确定具体原因,也不知道该如何修复,恳请帮助。

内容的提问来源于stack exchange,提问作者Eunseo Sim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 05:53:29