简体   繁体   中英

avoid passing native type in typescript in react component

I have a component that receive style or any other native html attribute, how can I avoid to declaring their obvious attribute type?

function Question({style}:{style:string}) {
  return (
    <div style={style}>
      something
    </div>
  );
}

I think I can extend HTMLsomething (I forgot), so that native type like style, type, name don't have to declare the obvious type.

First off, style , in particular, isn't a string in JSX. It's an object, like the style property on a DOM element (and not like the style attribute in HTML; JSX.= HTML).

If you want to declare that your component accepts all the usual HTML attributes, you can use the HTMLAttributes<T> type defined in React's index.d.ts (and/or SVGAttributes<T> for SVG). That's how React types its own DOMElement interface's props, for instance.

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