Skip to content

Instantly share code, notes, and snippets.

@pigoz
Created December 19, 2019 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pigoz/10606e5477f4ec384081b83577cf5c1e to your computer and use it in GitHub Desktop.
Save pigoz/10606e5477f4ec384081b83577cf5c1e to your computer and use it in GitHub Desktop.
type FunctionComponent<A> = (a: A) => string
function contramap<A, B>(fc: FunctionComponent<A>, f: (b: B) => A): FunctionComponent<B> {
return b => fc(f(b))
}
// --- esempio ---
interface _Props {
a: string
b: string
}
// lavoro con tutte le props required che è comodo
const _Foo: FunctionComponent<_Props> = ({ a, b }) => `<div >${a.trim()} ${b.trim()}</div>`
// interfaccia pubblica
export interface Props {
b: string
}
// logica diversa da Object.assign --------------------------v
export const Foo = contramap(_Foo, (x: Props) => ({ ...x, a: x.b }))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment