当前位置: 首页 > 知识库问答 >
问题:

如何使用Typescript扩展Material UI组件的props?

齐晟
2023-03-14

我想使用打字稿从材料-用户界面扩展按钮组件的道具,以便能够将额外的道具传递给它的孩子。

import { NavLink } from 'react-router-dom';
import { Button } from 'material-ui';

<Button
  component={NavLink}

  // NavLink props that needs to be added to typescript declarations
  activeClassName={classes.activeButton}
  exact={true}
  to={to}
>
  {title}
</Button>

我尝试在中添加以下声明。/app/types/material_ui.d.ts:

declare module 'material-ui' {
  interface Button {
    activeClassName?: any;
    exact?: boolean;
    to?: string;
  }
}

这将引发错误“TS2693:'Button'仅引用一种类型,但在此处用作值。

我也试过声明:

import * as React from 'react';

import { PropTypes, StyledComponent } from 'material-ui';
import { ButtonBaseProps } from 'material-ui/ButtonBase';

declare module 'material-ui' {
  export interface ButtonProps extends ButtonBaseProps {
    color?: PropTypes.Color | 'contrast';
    component?: React.ReactNode;
    dense?: boolean;
    disabled?: boolean;
    disableFocusRipple?: boolean;
    disableRipple?: boolean;
    fab?: boolean;
    href?: string;
    raised?: boolean;
    type?: string;

    activeClassName?: any;
    exact?: boolean;
    to?: string;
  }

  export class Button extends StyledComponent<ButtonProps> { }
}

这将引发错误“TS2323:无法重新声明导出的变量'按钮'”。

我的tsconfig.json是这样的:

{
  "compilerOptions": {
    ...
    "paths": {      
      "history": ["./node_modules/@types/history/index"],
      "redux": ["./node_modules/@types/redux/index"],
      "react": ["./node_modules/@types/react/index"],
      "*": ["./app/types/*", "*"]
    },
  },
  ...
}

最后是来自Menty-UI的原始类型定义:

import * as React from 'react';
import { StyledComponent, PropTypes } from '..';
import { ButtonBaseProps } from '../ButtonBase';

export interface ButtonProps extends ButtonBaseProps {
  color?: PropTypes.Color | 'contrast';
  component?: React.ReactNode;
  dense?: boolean;
  disabled?: boolean;
  disableFocusRipple?: boolean;
  disableRipple?: boolean;
  fab?: boolean;
  href?: string;
  raised?: boolean;
  type?: string;
}

export default class Button extends StyledComponent<ButtonProps> {}

我用的是material-ui 1 . 0 . 0-beta 8和react 15.6.1,react-router-dom 4.2.2和typescript 2.5.2。

共有3个答案

岳浩宕
2023-03-14

以下代码适合我

import { Button, StyledComponent } from 'material-ui';
import { ButtonProps } from 'material-ui/Button';

declare module 'material-ui' {
  export interface MyProps {

    exact?: boolean;
    to?: string;
  }
  export class Button extends StyledComponent<ButtonProps & MyProps> {
  }

}

我对“TS2693没有问题:“Button”仅指类型,但在这里用作值。,我也在使用Typescript 2.5.2

班泽语
2023-03-14

有点晚了,但这里有另一个解决方案。另请参见组件属性的MUI文档用法。以下按钮使用链接组件。它还将链接的特定属性用于它们的类型。它也添加了自己的特殊道具。

import React from 'react'
import MuiButton, {ButtonProps} from '@material-ui/core/Button'

interface ButtonOptions {
    // your custom options with their types
  }

const Button = <C extends React.ElementType>(props: ButtonProps<C, {component?: C}> & ButtonOptions ) => {
  return <MuiButton {...props}>{props.children}</MuiButton>
}

export default Button
 
// use like this:  
import {Link} from 'react-router-dom'
<Button component={Link} to={'/somelocation'}>
祁英哲
2023-03-14
import React, { FC } from 'react';

import { Button, ButtonProps } from '@material-ui/core';

interface IProps extends ButtonProps {} // your custom props

const ButtonHco: FC<IProps> = ({
  variant,
  color,
  children,
  size,
  disabled
}) => {
  return (
    <Button variant={variant} color={color} size={size} disabled={disabled}>
      {children}
    </Button>
  );
};

export default ButtonHco;
 类似资料:
  • 问题内容: 我正在将React.js与TypeScript一起使用。有什么方法可以创建从其他组件继承但具有一些其他道具/状态的React组件? 我想要达到的目标是这样的: 但是,如果我把这个会失败中,我得到一个错误的打字稿(类型的参数是不能分配给类型)。我想这不是TypeScript专用的东西,而是将继承与泛型(?)混合使用的一般限制。是否有任何类型安全的解决方法? 更新 我选择的解决方案(基于D

  • 问题内容: Typescript总是抱怨调色板中缺少某些属性。如果添加,我的应用程序运行正常,但显然我想避免这种情况。我是Typescript的新手,这是我尝试过的。 这会引发错误, 对我来说这是一个令人困惑的错误,因为类型I不在任何地方使用该类型。 如何在此处适当扩展调色板? 问题答案: 解 参考 如果我们看一下createMuiTheme.d.ts 我们会发现这一点,并发挥不同的作用。 主题:

  • 我有以下类 : 我想按如下方式使用它: 因此,通过扩展已经注入依赖项的超类,我希望在其子类中访问它。 我尝试了许多不同的方法,甚至将超类作为组件: 但是,<code>仍然是这样。fetchApi为,即使在超类中也是如此。

  • SpecializedButton FXML视图只创建一个HBox,其中有两个锚窗格,左右两侧分别有一个标签和按钮。单击按钮时,它调用SpecializedButton控制器类中的doSomething()。 问题 通过这个设置,我使用FXML将视图与应用程序逻辑分开。 我会非常感谢你的真知灼见。提前道谢!

  • 问题内容: 我正在尝试添加一个属性以使用Typescript从中间件表达请求对象。但是我不知道如何向对象添加额外的属性。如果可能的话,我宁愿不使用括号符号。 我正在寻找一种解决方案,允许我编写与此类似的内容(如果可能): 问题答案: 您想要创建一个自定义定义,并在Typescript中使用一个称为声明合并的功能。这是常用的,例如在中。 创建一个文件,并确保将其包含在您的-部分中(如果有)。内容如下

  • 问题内容: 我最欣赏Backbone.js的一件事是简单而优雅的继承是如何工作的。我开始着手处理React,并且在React中无法真正找到类似于此Backbone代码的任何内容 在react中,我们有mixin,如果使用mixin,我们可以像上面的例子那样有点接近 与一遍又一遍地定义相同的东西相比,这没有那么重复,但是它似乎不像Backbone那样灵活。例如,如果我尝试重新定义/覆盖存在于我的一个