繁体   English   中英

如何扩展本机组件打字稿

[英]How to extend react-native component typescript

我正在尝试编写文本组件以在整个React Native应用程序中使用

如下

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'
    }
});

实际上,我试图在单个位置应用样式,并在整个应用程序中使用它。

我的问题是打字稿编译器抱怨:

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

我想出了一个解决方案。 我所做的是。

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'
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM