Skip to content

ThemeProvider: theme accepts any value, and a partial theme crashes at render #5236

Closed

Description

@Doberjohn

Current behaviour

ThemeProvider's theme prop accepts any value, because src/theme/provider.tsx (line 14) creates it with createTheming<unknown>(LightTheme). At runtime, ThemeProvider puts that value into context as it is, with no merge. A partial theme reaches the components below it, and they crash at render:

<PaperProvider>
  <ThemeProvider theme={{ colors: { primary: 'red' } }}>
    <Button>Press me</Button>
  </ThemeProvider>
</PaperProvider>
TypeError: Cannot read properties of undefined (reading 'spring')

theme={42} also passes the type check and throws the same way. The same partial theme on PaperProvider renders, because PaperProvider has merged its theme onto the default since #5234. TypeScript also doesn't report a removed key such as roundness in an inline ThemeProvider theme, while it does report it on PaperProvider and on a component's theme prop.

Expected behaviour

ThemeProvider works like the other theme props. It merges its theme onto the theme above it with safeMerge, as a component's theme prop does, and its theme is typed ThemeProp. A full theme gives the same result as today, and a partial one renders. TypeScript reports keys the theme doesn't have.

PaperProvider renders this ThemeProvider with a theme it has already merged, so it could keep the unmerged provider internally. withTheme comes from the same createTheming call, so changing its type argument changes withTheme's typing too.

If the merge isn't wanted, typing theme as the full Theme would still turn the crash into a type error.

#5235 adds sentences to the migration guide that say TypeScript doesn't check ThemeProvider's theme. They need updating along with this.

How to reproduce?

  1. Render the snippet above on main, in an app or in a Jest test with @testing-library/react-native.
  2. Type-check <ThemeProvider theme={42}> or <ThemeProvider theme={{ roundness: 2 }}>. Neither gives an error, while <PaperProvider theme={{ roundness: 2 }}> gives TS2353.

What have you tried so far?

Spreading a full theme works: <ThemeProvider theme={{ ...LightTheme, colors: { ...LightTheme.colors, primary: 'red' } }}>. Passing on the result of useTheme() also works, as the Gotchas section of the theming guide shows. The unknown comes from #3279, when Paper had both MD2 and MD3 themes. 6.x has a single Theme. The 5.0 branch has the same createTheming<unknown> call.

Your Environment

software version
react-native-paper 6.0.0-alpha.3 (main at a525c28)
@callstack/react-theme-provider 3.0.9
typescript 5.8.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions