打字的样式主题问题(打字稿)

时间:2020-02-14 18:43:02

标签: typescript-typings styled-components

我在使用TypeScript预配置的react-native项目中将styled-componentsstyled-theming结合使用。

我从theme库中导出了styled-theming函数时遇到问题。它无法识别传递给该函数的道具。让我表明我的意思...

首先,这是我的package.json文件的一部分

"styled-components": "^5.0.1",
"styled-theming": "^2.2.0"

"@types/styled-components": "^4.4.3",
"@types/styled-theming": "^2.2.2",

"typescript": "^3.7.5"

第二,这是我的component.styles.ts文件

enter image description here

您可以在下面的图片中看到,props中的styled-components的键入是可以的。

enter image description here

但是它们没有在theme函数中使用

enter image description here

是否有解决此问题的想法?我希望在theme函数中键入正确的字符。

1 个答案:

答案 0 :(得分:0)

我不确定您的theme函数签名是什么样的,但是您可能会遇到this caveat,需要在其中显式地将主题设置为接口的参数。您可以尝试从styled-components导入主题对象并创建一个新函数-

import {DefaultTheme} from 'styled-components'

interface ThemeProps {
   theme: DefaultTheme
}

function setTextColor(props: ThemeProps){
   return theme('mode', {...implementation_here...})
}

export const TextStyled = styled.Text`
  color: ${props => setTextColor(props)}
`

相关问题