【问题标题】:How to use emotion/styled with React and Typescript如何使用 React 和 Typescript 使用情感/样式
【发布时间】:2021-03-29 11:49:54
【问题描述】:

我已将我的根组件包装在来自@emotion/reactThemeProvider 中,这样我就可以访问props.theme

const StyledDiv = styled.div`
  background-color: ${(props) => props.theme.palette.primary.main};
`;

问题: 根据 TS,props.theme 是一个空对象 - palette 在主题类型上不存在(但是我传递给主题提供者的对象确实包含该属性)。如果我让 TS 忽略这一点,代码就可以工作。

文档涵盖了这一点,并且看起来很容易解决: Extend the Emotion theme type

但是,我对文档的理解不足以使Theme 类型的props.theme 具有正确的属性。

非常感谢任何为我指明正确方向的帮助!

【问题讨论】:

    标签: reactjs typescript emotion


    【解决方案1】:

    添加此文件types.d.ts

    import "@emotion/react";
    import { IPalette } from "../your-palette";
    
    declare module "@emotion/react" {
      export interface Theme extends IPalette {}
    }
    

    【讨论】:

    • 在哪里添加这个文件?在项目文件夹的根目录上?
    猜你喜欢
    • 2021-11-01
    • 2017-11-12
    • 2021-11-13
    • 2021-03-30
    • 2019-12-15
    • 2018-06-09
    • 2020-09-17
    • 1970-01-01
    • 2020-09-29
    相关资源
    最近更新 更多