首页 > 解决方案 > 使用带有 typescript 的 injectGlobal 样式组件 API

问题描述

我正在尝试使用简单的 injectGlobal API,但无法使其与打字稿一起使用。我在 theme.tsx 中有以下设置

import * as styledComponents from "styled-components";
import { ThemedStyledComponentsModule } from "styled-components";
import IThemeInterface from "./theme";
const {
  default: styled,
  css,
  injectGlobal,
  keyframes,
  ThemeProvider
} = styledComponents as ThemedStyledComponentsModule<IThemeInterface>;
export default styled;
export { css, injectGlobal, keyframes, ThemeProvider }; 

在我的 App.tsx 中,我只是做

import { injectGlobal} from '../theme.tsx'

并尝试正常使用但总是出现以下错误

unused expression, expected an assignment or function call

寻求任何建议!

标签: reactjstypescriptstyled-components

解决方案


看起来这个错误来自tslint no-unused-expression ruleinjectGlobal您可以通过在您的 like 中添加allow-tagged-template选项来免除对模板标签的调用,tslint.conf例如:

{
    "rules": {
        "no-unused-expression": [true, "allow-tagged-template"]
        // ...
    }
}

推荐阅读