【问题标题】:TypeScript: Interface cannot simultaneously extends two typesTypeScript:接口不能同时扩展两种类型
【发布时间】:2019-04-07 16:18:43
【问题描述】:

我正在使用 TypeScript 编写一个 React 应用程序。我将 material-ui 用于我的组件。我正在为 material-ui 的 Button 组件编写自定义包装器。它看起来像这样:

import MUIButton, { ButtonProps } from "@material-ui/core/Button";
import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles";
import classNames from "classnames";
import React, { PureComponent } from "react";
import styles from "./styles";

export interface OwnProps {
  color: "primary" | "danger" | "warning" | "transparent";
  size: "sm" | "lg";
}

export interface Props
  extends WithStyles<typeof styles>,
    OwnProps,
    ButtonProps {}

export class Button extends PureComponent<Props> {
  render() {
    const { color, size, classes, children, ...rest } = this.props;
    const btnClasses = classNames({
      [classes.button]: true,
      [classes[size]]: size,
      [classes[color]]: color
    });
    return (
      <MUIButton {...rest} className={btnClasses}>
        {children}
      </MUIButton>
    );
  }
}

export default withStyles(styles)(Button);

问题是这里Props的定义抛出了错误信息:

Named property 'color' of types 'OwnProps' and 'ButtonProps' are not identical.
[ts]
Interface 'Props' cannot simultaneously extend types 'OwnProps' and 'ButtonProps'.
  Named property 'size' of types 'OwnProps' and 'ButtonProps' are not identical.
Named property 'color' of types 'OwnProps' and 'ButtonProps' are not identical.
[ts]
Interface 'Props' cannot simultaneously extend types 'OwnProps' and 'ButtonProps'.
  Named property 'size' of types 'OwnProps' and 'ButtonProps' are not identical.

如果我改写,这个错误就会消失:

export class Button extends PureComponent<Props & ButtonProps> {

但是当使用 Button 时,道具颜色和大小会抛出错误:

The expected type comes from property 'color' which is declared here on type 'IntrinsicAttributes & Pick<Props & ButtonProps, ...

我怎样才能正确地告诉组件它具有我定义的道具 (OwnProps) 以及像往常一样来自 Button 的道具?

【问题讨论】:

  • 请包括有错误的代码和错误的全文(或至少主要部分,“类型X不可分配给类型Y”)
  • @jcalz 我编辑了这个问题????谢谢你的帮助!

标签: reactjs typescript typescript-typings react-props typescript-types


【解决方案1】:
import { ButtonProps } from "@material-ui/core/Button";

export type OwnProps = Omit<ButtonProps, "color" | "size"> & {
  color: "primary" | "danger" | "warning" | "transparent";
  size: "sm" | "lg";
}

class MyButton extends React.Component<OwnProps> {


}

【讨论】:

  • 希望对你有帮助
  • 这适用于 OwnProps 警告,但现在对于 export interface Props extends WithStyles&lt;typeof styles&gt;, OwnProps {} 我收到错误: 和 'OwnProps' 不相同。` 类似于第二个我在我的问题中描述了。你知道如何解决这个问题吗?
  • Interface 'Props' cannot simultaneously extend types '{ classes: Record&lt;"primary" | "danger" | "warning" | "transparent" | "sm" | "lg" | "button", string&gt;; innerRef?: string | ((instance: any) =&gt; any) | RefObject&lt;any&gt; | undefined; }' and 'OwnProps'. Named property 'classes' of types '{ classes: Record&lt;"primary" | "danger" | "warning" | "transparent" | "sm" | "lg" | "button", string&gt;; innerRef?: string | ((instance: any) =&gt; any) | RefObject&lt;any&gt; | undefined; }
  • 是的,这是因为 mui 按钮需要自己的类,因此您也可以在 Omit 中排除“类”,然后它应该可以工作
  • 这个答案实际上并没有解释如何解决这个问题。解决方案对这个问题非常具体,这没关系,但添加解释将使其能够为围绕其问题的类似但独特情况的其他人提供价值。
猜你喜欢
  • 2020-02-19
  • 2021-08-07
  • 2015-04-01
  • 2018-06-13
  • 2021-12-21
  • 2022-12-06
  • 2021-06-09
  • 2018-04-09
  • 2018-06-20
相关资源
最近更新 更多