Skip to content

Instantly share code, notes, and snippets.

@KonradSzwarc
Created December 28, 2021 17:15
Show Gist options
  • Save KonradSzwarc/9cd280468f056e334f5787871e090efa to your computer and use it in GitHub Desktop.
Save KonradSzwarc/9cd280468f056e334f5787871e090efa to your computer and use it in GitHub Desktop.
Creates object with specyfied value types that can be extended by new key-value pairs.
const mapBuilder = <ValueType = never>() => {
const addFactory = <Values extends Record<string, ValueType>>(values: Values) => ({
add: <Key extends string>(key: Key, data: ValueType) =>
addFactory({ ...values, [key]: data } as Record<keyof Values | Key, ValueType>),
build: () => values,
});
return addFactory({});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment