Skip to content

Instantly share code, notes, and snippets.

@typoerr
Last active June 16, 2023 16:37
Show Gist options
  • Save typoerr/865844d4143b951adf8e9419b6a8298f to your computer and use it in GitHub Desktop.
Save typoerr/865844d4143b951adf8e9419b6a8298f to your computer and use it in GitHub Desktop.
Typesafe immutable set with lodash/fp/set
/* tslint:disable:max-line-length */
const _ = {
set: require('lodash/fp/set'),
};
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], K6 extends keyof O[K1][K2][K3][K4][K5], K7 extends keyof O[K1][K2][K3][K4][K5][K6], K8 extends keyof O[K1][K2][K3][K4][K5][K6][K7], K9 extends keyof O[K1][K2][K3][K4][K5][K6][K7][K8], K10 extends keyof O[K1][K2][K3][K4][K5][K6][K7][K8][K9], V>(obj: O, path: string | [K1, K2, K3, K4, K5, K6, K7, K8, K9, K10], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, Record<K5, Record<K6, Record<K7, Record<K8, Record<K9, Record<K10, V>>>>>>>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], K6 extends keyof O[K1][K2][K3][K4][K5], K7 extends keyof O[K1][K2][K3][K4][K5][K6], K8 extends keyof O[K1][K2][K3][K4][K5][K6][K7], K9 extends keyof O[K1][K2][K3][K4][K5][K6][K7][K8], V>(obj: O, path: string | [K1, K2, K3, K4, K5, K6, K7, K8, K9], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, Record<K5, Record<K6, Record<K7, Record<K8, Record<K9, V>>>>>>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], K6 extends keyof O[K1][K2][K3][K4][K5], K7 extends keyof O[K1][K2][K3][K4][K5][K6], K8 extends keyof O[K1][K2][K3][K4][K5][K6][K7], V>(obj: O, path: string | [K1, K2, K3, K4, K5, K6, K7, K8], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, Record<K5, Record<K6, Record<K7, Record<K8, V>>>>>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], K6 extends keyof O[K1][K2][K3][K4][K5], K7 extends keyof O[K1][K2][K3][K4][K5][K6], V>(obj: O, path: string | [K1, K2, K3, K4, K5, K6, K7], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, Record<K5, Record<K6, Record<K7, V>>>>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], K6 extends keyof O[K1][K2][K3][K4][K5], V>(obj: O, path: string | [K1, K2, K3, K4, K5, K6], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, Record<K5, Record<K6, V>>>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], K5 extends keyof O[K1][K2][K3][K4], V>(obj: O, path: string | [K1, K2, K3, K4, K5], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, Record<K5, V>>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], K4 extends keyof O[K1][K2][K3], V>(obj: O, path: string | [K1, K2, K3, K4], val: V): O & Record<K1, Record<K2, Record<K3, Record<K4, V>>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], K3 extends keyof O[K1][K2], V>(obj: O, path: string | [K1, K2, K3], val: V): O & Record<K1, Record<K2, Record<K3, V>>>;
function set<O, K1 extends keyof O, K2 extends keyof O[K1], V>(obj: O, path: string | [K1, K2], val: V): O & Record<K1, Record<K2, V>>;
function set<O, K1 extends keyof O, V>(obj: O, path: string | [K1], val: V): O & Record<K1, V>;
function set<O, K1 extends keyof O, V>(obj: O, path: string | K1, val: V): O & Record<K1, V> {
return _.set(path, val, obj);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment