I started with typescript for react native, but sometimes I get very weird error messages and I am not able to fix them.
I am just playing around a bit and I have this line in my code:
<Text text={user?.last_name} style={{color:colors.textSecondary}} variant="labelMedium"/>
Visual Studio Code is giving me this error for colors.textSecondary:
Property 'textSecondary' does not exist on type '{ primary: string; background: string; card: string; text: string; border: string; notification: string; }'.ts(2339)
If I use colors.text it is working, but I do not understand why. Here is the theme file, where I am getting the standard theme colors from:
export const Theme = {
light: {
mode: 'light',
colors: {
primary: '#1976d2',
secondary: '#7393B3',
background: '#f9f9f9',
card: '#ffffff',
text: '#303233',
textSecondary: '#727272',
border: '#e8e8e8',
error: '#f44336',
},
},
dark: {
mode: 'dark',
colors: {
primary: '#1976d2',
secondary: '#7393B3',
background: '#171819',
card: '#202122',
text: '#e4e6eb',
textSecondary: '#b0b3b8',
border: '#333435',
error: '#f44336',
},
},
};
What am I doing wrong here? The text color is displaying correct by the way.
Thanks! Jan
The issue is because the React Native Theme
has a set number of properties as defined:
The additional theme value you added is not known to typescript, you have are adding a superset of that value. By adding an additional property in your theme your consuming code has no knowledge that that prop exists. It is only aware of the base Theme props React defined.
Unfortunately looking at the library I do not see no great way of extending the Theme type available properties (some comments propose some nice ideas). I would recommend the following options
style={{color:colors['textSecondary']}}
import {Theme, useTheme} from "@react-navigation/native";
type SyblerTheme = Theme & {
colors: {
textSecondary: string;
};
};
export function useSyblerTheme(): SyblerTheme {
return useTheme() as any;
}