import React, { ComponentType, FunctionComponent } from "react"; import { Call, Fn, KeepNeversFn, OmitFn } from "../types/Fn"; import { Hoc } from "../types/Hoc"; import { newHoc } from "../utils/newHoc"; interface WithOmitFn extends Fn { return: Call< KeepNeversFn>, { [K in Extract[0]]: [ K, K extends OmitNames ? undefined extends Extract[1] ? Extract[1] : never : Extract[1], ]; }[Extract[0]] >; } type WithOmitHoc = ( omitNames: OmitNames[], ) => Hoc<[WithOmitFn]>; /** * Omit, which is actually ignore mentioned props * * @see {@link withPick} * @example * const user = { * username: "Some username", * password: "s0m3 p4ssw0rd", * vitalSecretInfo: "..." * }; * function RevealInfo( * { username, password, vitalSecretInfo }: * { username: string; password?: string; vitalSecretInfo: string } * ) { * ... * } * const SafeRevealInfo = withOmit(["password"])(RevealInfo); * * // is equivalent to * // password is not included, but you can still see vitalSecretInfo */ export const withOmit = newHoc(function withOmit( Component: ComponentType, omitNames: string[], ): FunctionComponent { const omitSet = new Set(omitNames); return function WithOmit(props: any): JSX.Element { const newProps = { ...props }; for (const key in newProps) { if (omitSet.has(key)) { delete newProps[key]; } } return ; }; });