Created
December 28, 2021 17:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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