[React Typescript] ElementType, ComponentType
The ElementType
type 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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源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