简体   繁体   English

如何从“material-ui”导入类型?

[英]How to import types from `material-ui`?

I am working on a react app with material-ui in typescript .我正在使用material-ui中的typescript开发一个react应用程序。 I wonder where I can find all types definition for material component.我想知道在哪里可以找到material组件的所有类型定义。 I have tried to install @types/material-ui but it doesn't work well.我尝试安装@types/material-ui但效果不佳。 See below example:请参见下面的示例:

import * as React from 'react';
import SvgIcon from '@material-ui/core/SvgIcon';
import { SvgIconProps } from "material-ui";

export const Logo = (props: SvgIconProps) => (
  <SvgIcon {...props}>
    <path d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z" />
  </SvgIcon>
);

I got below error on compilation:我在编译时遇到以下错误:

Type '{ children: Element; color?: string | undefined; hoverColor?: string | undefined; onMouseEnter?: ((event: MouseEvent<{}, MouseEvent>) => void) | undefined; onMouseLeave?: ((event: MouseEvent<{}, MouseEvent>) => void) | undefined; ... 413 more ...; ref?: string | ... 3 more ... | undefined; }' is not assignable to type 'SvgIconProps'.
  Types of property 'color' are incompatible.
    Type 'string | undefined' is not assignable to type '"inherit" | "default" | "disabled" | "error" | "primary" | "secondary" | "action" | undefined'.
      Type 'string' is not assignable to type '"inherit" | "default" | "disabled" | "error" | "primary" | "secondary" | "action" | undefined'.

Specific props can be imported material UI itself. 特定道具可以导入材质UI本身。

For SvgIcon component from the question, the Prop Typing can be imported with: 对于问题中的SvgIcon组件,可以使用以下命令导入Prop Typing

import SvgIcon, { SvgIconProps } from '@material-ui/core/SvgIcon';

In material-ui v5 (mui) it can be imported like so:在 material-ui v5 (mui) 中,它可以像这样导入:

import { SvgIconProps } from '@mui/material';

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

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