I am trying this library fast-memoize
to generate style for Text
React-Native
.
On the code below, use the color parameter to generate the string color.
import { StyleSheet, TextStyle } from 'react-native';import memoize from 'fast-memoize';import { Colors } from '../../../Constants';import type { Styles, Color } from './types';export default StyleSheet.create<Styles>({ textStyles: memoize( (color: Color): TextStyle => ({ color: color ? Colors[color] : Colors.BLACK, fontSize: 12, }), ),});
On this code, will define the type of the Styles.
import { TextStyle } from 'react-native';import { Colors } from '../Constants';export type Styles = { textStyles: (color: string) => TextStyle;};
But it will return an error when checking the type code.
Type 'Styles' does not satisfy the constraint 'NamedStyles<any> | NamedStyles<Styles>'. Type 'Styles' is not assignable to type 'NamedStyles<Styles>'. Types of property 'textStyles' are incompatible. Type '(color: string) => TextStyle' is not assignable to type 'ViewStyle | TextStyle