Below have I a very simple function, which I type check at compile and runtime, but I would to also rumtime check all the keys result in a value, and if it doesn't, then prompt where it broke.
The only solution I can think of is
assert(typeof db[customer] !== undefined);
assert(typeof db[customer].offers !== undefined);
assert(typeof db[customer].offers[offerId] !== undefined);
assert(typeof db[customer].offers[offerId][key] !== undefined);
but it is a lot of typing. I expected lodash would have a solution for this problem, but doesn't seem to be the case.
If I do
db?[customer]?.offers?[offerId]?[key] = value;
then I don't know where it returned undefined.
import assert from 'assert';
export function setPropertyOffer(
db: Record<string, any>,
customer: number,
offerId: number,
key: string,
value: number | string,
) {
assert(typeof db === 'object');
assert(typeof customer === 'number');
assert(typeof offerId === 'number');
assert(typeof key === 'string');
assert(typeof value === 'string' || typeof value === 'number');
db[customer].offers[offerId][key] = value;
}