xerotolerant
xerotolerant

Reputation: 2079

How to extend react-native component typescript

I am trying to compose text components to use throughout a react native application

it is as follows

import React from 'react';
import { Text, TextProperties, StyleSheet } from 'react-native';
import Colors from '../Colors';

export default (props: TextProperties) => <Text style={styles.text} {...props} />

const styles = StyleSheet.create({
    text: {
        color: Colors.primary,
        fontSize: 24,
        fontWeight: 'bold'
    }
});

In effect I am trying to apply styles in a single location and use it throughout the applicaiton.

my issue is that the typescript compiler is complaining:

 error TS2559: Type '{ children: string; }' has no properties in common with type 'IntrinsicAttributes & TextProperties'.

Upvotes: 2

Views: 1494

Answers (1)

xerotolerant
xerotolerant

Reputation: 2079

I figured out a solution. What I did was.

import React from 'react';
import { Text, TextProperties, StyleSheet } from 'react-native';
import Colors from '../Colors';

export default (props: Props) => <Text style={styles.text} {...props} />

interface Props extends TextProperties {
   children: string;
}
const styles = StyleSheet.create({
    text: {
        color: Colors.primary,
        fontSize: 24,
        fontWeight: 'bold'
    }
});

Upvotes: 3

Related Questions