如何在打字稿中继承React SectionList prop类型?

时间:2020-05-27 04:53:59

标签: reactjs typescript react-native react-props

我正在SectionList周围创建一个包装器,该包装器应包含SectionList所需要的所有道具以及我的自定义道具。我该如何配置打字稿?

这是我的尝试:

import React from 'react';
import { SafeAreaView, SectionList } from 'react-native';

interface EnhancedSectionListProps {
  ...SectionListProps;   // this obviously won't compile, but shows what I'm trying to achieve
  enhancedProp: string;
}

export const EnhancedSectionList = (props: EnhancedSectionListProps) => {
  return (
    <SafeAreaView>
      <SectionList {...props} />
      // use my `enhancedProp` here
    </SafeAreaView>
  );
};

PS:我们使用prop-types库。

1 个答案:

答案 0 :(得分:1)

要完成任务,您需要扩展界面。例如:

interface EnhancedSectionListProps extends SectionListProps {
  enhancedProp: string;
}