简体   繁体   English

如何让 React 项目中的 Emotion 样式组件与 TypeScript 一起使用?

[英]How to get Emotion styled component in a React project to work with TypeScript?

I'm learning TypeScript and am trying to convert a tiny project that uses Emotion to TypeScript.我正在学习 TypeScript,并试图将一个使用 Emotion 的小项目转换为 TypeScript。

I've gotten stuck on the following point.我陷入了以下几点。

The following code以下代码

export const Title = styled.div(props => ({
    fontSize: "20px",
    color: props.color,
    fontWeight: "700"
}));

gives the error给出错误

TS2345: Argument of type '(props: Pick, HTMLDivElement>, "color" | "hidden" | "style" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 247 more ... | "css"> & { ...; }) => { ...; TS2345:类型参数 '(props: Pick, HTMLDivElement>, "color" | "hidden" | "style" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 247 more ... | "css" > & { ...; }) => { ...; }' is not assignable to parameter of type 'TemplateStringsArray'. }' 不能分配给 'TemplateStringsArray' 类型的参数。 Type '(props: Pick, HTMLDivElement>, "color" | "hidden" | "style" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 247 more ... | "css"> & { ...; }) => { ...;输入 '(props: Pick, HTMLDivElement>, "color" | "hidden" | "style" | "defaultChecked" | "defaultValue" | "suppressContentEditableWarning" | ... 247 more ... | "css"> & { . ..; }) => { ...; }' is missing the following properties from type 'TemplateStringsArray': raw, concat, join, slice, and 18 more. }' 缺少来自类型 'TemplateStringsArray' 的以下属性:raw、concat、join、slice 等 18 个。 ⌥⇧⏎ ⌥⏎ ⌥⇧⏎⌥⏎

I did some googling and added我做了一些谷歌搜索并添加

   "types": ["@emotion/core","@emotion/styled"],                           /* Type declaration files to be included in compilation. */

to my compiler options in my tsconfig.json , as well as trying to add a type for the props to the styled component like this到我的tsconfig.json编译器选项,以及尝试将道具的类型添加到样式组件中,如下所示

type StyleProps = {
    [k: string]: string | undefined;
}
export const Title = styled.div<StyleProps>(props => ({
    fontSize: "20px",
    color: props.color,
    fontWeight: "700"
}));

That didn't work.那没有用。

The funny thing is it seems it's only the line有趣的是它似乎只是线路

    fontWeight: "700"

that is the issue here.这就是这里的问题。 The following works fine以下工作正常

export const Container = styled.div(props => ({
    backgroundColor: props.color,
    padding: "20px",
    borderRadius: "14px",
    marginBottom: "20px",
    border: "1px solid rgba(100,100,50,0.5)",

}));

But balks at other fields added.但对添加的其他领域犹豫不决。 What is it one needs to do to get Emotion to work with TS?需要做什么才能让 Emotion 与 TS 一起工作? And, just as important, what is going on here?而且,同样重要的是,这里发生了什么?

I've worked with typed languages before, but feel like integrating TypeScript with other libraries is extraordinarily opaque.我以前使用过类型语言,但感觉将 TypeScript 与其他库集成是非常不透明的。 I've already abandoned PropTypes from the project, even if they provide runtime checking that TS doesn't, because it was tricky to get them to work with TS.我已经从项目中放弃了 PropTypes,即使它们提供了 TS 没有的运行时检查,因为让它们与 TS 一起工作很棘手。

Any help much appreciated!非常感谢任何帮助!

You can use:您可以使用:

interface StyleProps {
   color: string;
}    

export const Title = styled.div<StyleProps>`
   font-size: 20,
   color: ${color},
   font-weight: 700
`;

You can declare the type directly if you don't need to re-use it.如果不需要重复使用,可以直接声明类型。 I also find that destructuring the props and using the template literals makes it easier to work on longer CSS declarations.我还发现解构 props 并使用模板文字可以更轻松地处理更长的 CSS 声明。

export const Title = styled.div<{color: string}>(({color}) => `
    font-size: 20px;
    color: ${color};
    font-weight: 700;
`);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM