【问题标题】:declaration merging for react-native-elements themereact-native-elements 主题的声明合并
【发布时间】:2021-04-15 14:41:28
【问题描述】:

我在我的 react-native 应用程序中使用 react-native-elements。

我的应用使用 ThemeProvider 进行包装,以将主题传递给所有组件。

<SafeAreaProvider>
 <ThemeProvider theme={Theme}>
    <Loader visible={loader.loading} text={loader.message} absolute={true} />
    <RootNavigation />
  </ThemeProvider>
</SafeAreaProvider>

在主题文件中,我定义了我想在整个应用程序中使用的值。

const theme = {
  colors: {
    primary: '#6A69E2',
    primaryDark: '#4747c2',
    primaryLight: 'rgba(106, 105, 226, 0.35)',
    gray: {
      dark: '#242424',
      default: '#666',
      medium: '#999',
      light: '#ccc',
      lightest: '#e7e7e7',
    },
  },
  text: {
    size: {
      small: 12,
      default: 16,
      large: 18,
      h1: 26,
      h2: 22,
      h3: 20,
    },
  },
  Text: {
    style: {
      fontSize: 16,
      color: '#242424',
      fontFamily: 'Roboto',
    },
  },
  Button: {
    style: {
      borderRadius: 50,
    },
    disabledStyle: {
      backgroundColor: 'rgba(106, 105, 226, 0.35)',
    },
  },
};

export default theme;

对于值,提供此功能的 react-native-elements 的原始主题是有效的。例如,我可以通过使用访问颜色

const theme = useTheme()
theme.colors.primary

但是当我想添加一些新的属性,比如 primaryDark 时,我会得到一个 linter 错误。

Object literal may only specify known properties, and 'primaryDark' does not exist in type 'RecursivePartial<Colors>'.ts(2322)

在 react-native-elements 的文档中是关于声明合并的一部分,但我不明白如何存档 https://reactnativeelements.com/docs/customization/#typescript-definitions-extending-the-default-theme.

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 什么是 linter 错误?
  • 对象字面量只能指定已知属性,并且'recursivePartial'类型中不存在'primaryDark'.ts(2322)
  • 看起来他们刚刚修复了它#2964

标签: javascript typescript react-native react-native-elements


【解决方案1】:

好吧,声明合并仍然有效。这似乎是 lib 的一个错误。

他们的文档说您可以在module 'react-native-elements' 中扩充Color 接口。但是目前(截至2021-04-18,使用v3.3.2)该接口实际上隐藏在module 'react-native-elements/dist/config/colors'中,而不是直接暴露在顶层,很奇怪。

我建议你向他们的 repo 提交问题。 没关系,已经有人filed the issue。

在我的机器上测试,以下解决方案有效。

import React from 'react'
import { useTheme, ThemeProvider } from 'react-native-elements'

declare module 'react-native-elements/dist/config/colors' {
  export interface Colors {
    primaryDark: string
    primaryLight: string
  }
}

const ChildComp = () => {
  const theme = useTheme()
  theme.theme.colors.primaryDark // <-- No more error ?
  return <div>foobar</div>
}

回复 OP 的评论。您可以随心所欲地扩充界面,只要之前不存在扩充的键即可。例如将foobar 键添加到FullTheme。

declare module 'react-native-elements' {
  export interface FullTheme {
    foobar: string
  }
}

【讨论】:

  • 感谢您的回答,因为颜色有效。但是,例如,我如何合并一些未在 FullTheme 中定义的其他属性?例如,如果我想拥有类似 theme.theme.globalStyle 的东西
  • 转到此文件node_modules/react-native-elements/dist/config/theme.d.ts。您将看到所有主题相关界面的位置。找到它们,并一一应用相同的技巧。这是一个不幸的情况,我知道?。
  • 哦没仔细看。你想增加FullTheme 本身吗?同样的,增加interface FullTheme {} 就像Colors 一样。
  • 这就是我之前尝试过的方式,但是我会在我的 theme.ts 中为主题出现一个新错误 =>“类型 [.... 主题属性....] 丢失'FullTheme' 类型的以下属性:Avatar、Accessory、Badge、BottomSheet 等 29 种。”
  • 发布更多细节。我没有看到上下文。猜不出原因。
猜你喜欢
  • 2022-10-25
  • 2020-07-02
  • 1970-01-01
  • 1970-01-01
  • 2023-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多