diff --git a/types/assoc.d.ts b/types/assoc.d.ts index cebbd9d..1a9a49c 100644 --- a/types/assoc.d.ts +++ b/types/assoc.d.ts @@ -1,97 +1,61 @@ import { Placeholder } from './util/tools'; // assoc(prop) -export function assoc(prop: K): { - // assoc(prop)(val) +export function assoc(prop: K): { + // assoc(prop)(val)(obj) (val: T): { - // assoc(prop)(val)(obj) when obj has key prop and val is typeof obj[prop] - >(obj: U): U; - // assoc(prop)(val)(obj) when obj has key prop and val is not typeof obj[prop] - >(obj: U): Record & Omit; - // assoc(prop)(val)(obj) when obj does not have key prop - (obj: U): U & Record; + (obj: U): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; } - // assoc(prop)(__, obj) when prop is keyof obj - >(__: Placeholder, obj: U): { - // assoc(prop)(__, obj)(val) if val is typeof obj[prop] - (val: T): U; - // assoc(prop)(__, obj)(val) if val is not typeof obj[prop] - (val: T): Record & Omit; + // assoc(prop)(__, obj)(val) + (__: Placeholder, obj: U): { + (val: T): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; } - // assoc(prop)(__, obj) when prop is not keyof obj - (__: Placeholder, obj: U): (val: T) => U & Record; - // assoc(prop)(val, obj) when obj has key prop, tests if val is typeof obj[prop] for best return type - >(val: T, obj: U): U[K] extends T ? U : Record & Omit; - // assoc(prop)(val, obj) when obj does not have a key prop - (val: T, obj: U): U & Record; + // assoc(prop)(val, obj) + (val: T, obj: U): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; }; // assoc(__, val) export function assoc(__: Placeholder, val: T) : { - // assoc(__, val)(prop) - (prop: K): { - // assoc(__, val)(prop)(obj) when obj has key prop, tests if val is typeof obj[prop] for best return type - >(obj: U): U[K] extends T ? U : Record & Omit; - // assoc(__, val)(prop)(obj) when obj does not have key prop - (obj: U): U & Record; + // assoc(__, val)(prop)(obj) + (prop: K): { + (obj: U): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; } - // assoc(__, val)(__, obj) + // assoc(__, val)(__, obj)(prop) (__2: Placeholder, obj: U): { - // assoc(__, val)(__, obj)(prop), prop is keyof obj, tests if val is typeof obj[prop] for best return type - (prop: K): U[K] extends T ? U : Record & Omit; - // assoc(__, val)(__, obj)(prop), prop is not keyof obj - (prop: K): U & Record; + (prop: K): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; }; - // assoc(__, val)(prop, obj), when obj has key prop, tests if val is typeof obj[prop] for best return type - (prop: K, obj: U): U[K] extends T ? U : Record & Omit; - // assoc(__, val)(prop, obj), when obj does not have key prop - (prop: K, obj: U): U & Record; + + // assoc(__, val)(prop, obj) + (prop: K, obj: U): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; }; -// assoc(prop, val) -export function assoc(prop: K, val: T) : { - // assoc(prop, val)(obj), when obj has key prop, tests if val is typeof obj[prop] for best return type - >(obj: U): U[K] extends T ? U : Record & Omit; - // assoc(prop, val)(obj), when obj does not have key prop - (obj: U): U & Record; +// assoc(prop, val)(obj) +export function assoc(prop: K, val: T): { + (obj: U): U extends Record ? U[K] extends T ? U : Record & Omit : U & Record; }; // assoc(__, __, obj) export function assoc(__: Placeholder, __2: Placeholder, obj: U): { - // assoc(__, __, obj)(key) - (key: K): { - // assoc(__, __, obj)(key)(value), when K is keyof U and value types match - (val: T): U; - // assoc(__, __, obj)(key)(value), when K is keyof U and value types do not match - (val: T): Record & Omit + // assoc(__, __, obj)(key)(val) + (key: K): { + (val: T): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; } - // assoc(__, __, obj)(key)(value), when K is not keyof U - (key: K): (val: T) => U & Record; - // assoc(__, __, obj)(__, value) + // assoc(__, __, obj)(__, value)(key) (__: Placeholder, val: T): { - // assoc(__, __, obj)(__, value)(key), when obj has key prop, tests if val is typeof obj[prop] for best return type - (key: K): U[K] extends T ? U : Record & Omit; - // assoc(__, __, obj)(__, value)(key) when obj does not have key prop - (key: K): U & Record; + (key: K): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; } // assoc(__, __, obj)(key, value) - (key: K, val: U[K]): U; - (key: K, val: T): Record & Omit; - (key: K, val: T): U & Record; + (key: K, val: T): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; }; -// assoc(__, val, obj)(prop), this tests if prop is keyof obj and if val is typeof obj[prop] for best return type -export function assoc(__: Placeholder, val: T, obj: U): (prop: K) => K extends keyof U ? T extends U[K] ? U : Record & Omit : U & Record; -// assoc(prop, __, obj)(val), when K is keyof obj, tests if val is typeof obj[prop] for best return type -export function assoc(prop: K, __: Placeholder, obj: U): (val: T) => T extends U[K] ? U : Record & Omit; -// assoc(prop, __, obj)(val), when prop is not keyof obj -export function assoc(prop: K, __: Placeholder, obj: U): (val: T) => U & Record; -// assoc(prop, val, obj) when prop is keyof obj and val is same type -export function assoc(prop: K, val: U[K], obj: U): U; -// assoc(prop, val, obj) when prop is keyof obj and val is not same type -export function assoc(prop: K, val: T, obj: U): Record & Omit; -// assoc(prop, val, obj) when prop is not keyof obj -export function assoc(prop: K, val: T, obj: U): U & Record; +// assoc(__, val, obj)(prop) +export function assoc(__: Placeholder, val: T, obj: U): (prop: K) => K extends keyof U ? T extends U[K] ? U : Record & Omit : U & Record; + +// assoc(prop, __, obj)(val) +export function assoc(prop: K, __: Placeholder, obj: U): (val: T) => K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record; + +// assoc(prop, val, obj) +export function assoc(prop: K, val: T, obj: U): K extends keyof U ? U[K] extends T ? U : Record & Omit : U & Record;