简体   繁体   中英

How to import types from `material-ui`?

I am working on a react app with material-ui in typescript . I wonder where I can find all types definition for material component. I have tried to install @types/material-ui but it doesn't work well. 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.

For SvgIcon component from the question, the Prop Typing can be imported with:

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

In material-ui v5 (mui) it can be imported like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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