[React Typescript] ElementType, ComponentType

The ElementTypetype helper is a bit unusal because it accepts some props and derives what types of components would be able to recieve those props.

Here we have an Example component that accepts audio and video as well as ComponentType<P>.

When we pass it an autoPlay prop:

export type Example = React.ElementType<{
  autoPlay?: boolean;
}>;

// hovering over Example shows:
type Example =
  | "audio"
  | "video"
  | React.ComponentType<{
      autoPlay?: boolean | undefined;
    }>;

ElementType can tell that this prop can be used with an audio or video element, or a React Component Type that accepts autoPlay as a prop.

Any custom component that you think of will correspond to ElementType, as well as any other element defined in JSX.IntrinsicElements that can receive props.

For example, many elements can accept an href prop. If you defined an ElementType to accept an optional href, all of a sudden there are several possible elements that can be used:

type Example = React.ElementType<{
  href?: string;
}>;

// hovering over Example shows:

type Example = "symbol" | "a" | "area" | "base" | "link" |
"svg" | "animate" | "animateMotion" | "animateTransform" |
"circle" | "clipPath" | "defs" | "desc" | "ellipse" |
"feBlend" | "feColorMatrix" | ... 46 more ...
| React.ComponentType<...>

 

 

ComponentType

Here we have a FuncComponent and a ClassComponent that take a prop1 that is a string:

const FuncComponent = (props: { prop1: string }) => {
  return null;
};

class ClassComponent extends React.Component<{
  prop1: string;
}> {
  render(): React.ReactNode {
    this.props.prop1;
    return null;
  }
}

By using the ComponentType type helper, you can ensure that only components that accept prop1 are passed to this array as seen in tests2:

const tests2: Array<React.ComponentType<{ prop1: string }>> = [
  FuncComponent,
  ClassComponent,
];

If we change the prop name in ClassComponent to propTwo, we will see an error because propOne is not assignable to propTwo:

class ClassComponent extends React.Component<{
  prop2: string;
}> {
  render(): React.ReactNode {
    this.props.prop2;
    return null;
  }
}

const tests2: Array<React.ComponentType<{ prop1: string }>> = [
  FuncComponent,
  ClassComponent, // Error!
];

// Error message on ClassComponent:
// Type 'ComponentType<{ prop2: string; }>' is not assignable to type 'ComponentType<{ prop1: string; }>'.

This demonstrates that ComponentType works can represent either functional or class components.

ComponentType is useful when working with higher-order components when you want to ensure that a specific prop type is being passed to a component.

posted @   Zhentiw  阅读(169)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2022-08-23 [Unit testing Express] Testing controller
2022-08-23 [Express] Extends CrudController with Mongoose model
2022-08-23 [Unit Testing] Test Mongoose model
2020-08-23 [React] Use react styled system with styled components
2019-08-23 [Cypress] install, configure, and script Cypress for JavaScript web applications -- part4
2018-08-23 [Debug] Inspect and Style an Element in DevTools that Normally Disappears when Inactive
2017-08-23 [Web Security] JSON Hijacking
点击右上角即可分享
微信分享提示