import React, { ComponentType, FunctionComponent } from "react"; import { HasAllPropsFn, IfThenFn, IntersectionFn, KeepNeversFn, OmitFn, } from "../types/Fn"; import { Hoc } from "../types/Hoc"; import { newHoc } from "../utils/newHoc"; type WithOverrideHoc = ( propName: PropName, value: PropValue, ) => Hoc< [ IfThenFn< HasAllPropsFn, [ ...(PropValue extends (...args: any[]) => any ? [] : [IntersectionFn<[PropName, PropValue]>]), KeepNeversFn>, ] >, ] >; /** * adds a prop value to the component, the given value can not be override, which means the given value overrides others * @see {@link withOverrides} {@link withFactory} {@link withDefault} {@link withDefaults} * @example * function CardWithIcon({ content, icon }: { * content: string; * icon: "star" | "heart" | ...; * }) { * return <>... * } * const StandardCard = withOverride("icon", "star")(CardWithIcon); * * * // is equivalent to * * * // and * // ❌ is a typescript error, but even though * // is equivalent to * */ export const withOverride = newHoc(function withOverride( Component: ComponentType, propName: string, value: any, ): FunctionComponent { return function WithOverride(props: any): JSX.Element { return ; }; });