Let's say you're creating a component that has all the props of input
but needs to add a label
prop. You'll need to extend from the ComponentProps
type helper
import { ComponentProps } from "react";
export type InputProps =
ComponentProps<"input"> & {
label: string;
};
export function Input({
label,
...props
}: InputProps) {
return (
<label>
{label}
<input {...props} />
</label>
);
}
But unfortunately, intersections used this way will, on the scale of a large codebase, slow TypeScript down.
Instead, you should use an interface, using interface extends
:
import { ComponentProps } from "react";
export interface InputProps
extends ComponentProps<"input"> {
label: string;
}
See article for more details
- performance Typescript Interface Type andperformance typescript interface type typescript interface type typescipt interface class type interface program and sv interface program3 program and interface type typescript this type type typescript expression error typescript predicates type all 关键字 关键typescript interface