Styled components's 'css' prop with TypeScript

落爺英雄遲暮 提交于 2021-01-28 12:01:17

问题


styled-components have a plugin that allows for the following:

<div
  css={`
    background: papayawhip;
    color: ${props => props.theme.colors.text};
  `}
/>

Is there any way I can tell TypeScript css is a valid property on all elements?


回答1:


Add following line to a TypeScript file inside your project as described in this issue:

// e.g. src/global.d.ts

import {} from "styled-components/cssprop"
// or TS 3.8+
import type {} from "styled-components/cssprop"

Alternatively you can manually augment the react module type declaration - copy/paste contents from "styled-components/cssprop" to above file:

import { CSSProp } from "styled-components"

interface MyTheme {} // declare custom theme type

declare module "react" {
  interface Attributes {
    css?: CSSProp<MyTheme>
  }
}

Latter variant will also allow you to customize the css prop theme type.



来源:https://stackoverflow.com/questions/60952710/styled-componentss-css-prop-with-typescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!