创建一个类,在运行时使用Typescript扩展另一个类

时间:2018-10-15 08:21:00

标签: javascript reactjs typescript generics react-apollo

我有以下内容:

class PeopleByNameQueryComponent extends Query<FindPeopleByNameQuery, FindPeopleByNameQueryVariables> { }


export const PeopleByNameQuery = (props: any) => {
    const { children, ...otherProps } = props;
    return <PeopleByNameQueryComponent query={FIND_PEOPLE_QUERY} variables={props.variables} {...otherProps}>
...
    </PeopleByNameQueryComponent>;
}

我想将代码简化为:

export const PeopleByNameQuery = (props: any) => {
    const { children, ...otherProps } = props;

    const ClassToUse = GenericQueryClass extends Query<props.FindQuery, props.QueryVariables> { }

    return <ClassToUse query={FIND_PEOPLE_QUERY} variables={props.variables} {...otherProps}>
    </ClassToUse>;
}

我不太确定这是否可行,最终目标是减少为完成同一件事而编写的代码量,因此会有很多查询。

0 个答案:

没有答案