Types for deep merging defaults in TS
export const deepMergeDefaults = <T>(
defaultObj: {
[k: string]: unknown;
},
passedObj: { [k: string]: unknown }
) => {
let result: typeof defaultObj = {};
for (let [key, value] of Object.entries(defaultObj)) {
if (!passedObj[key as keyof typeof passedObj]) {
result[key] = value;
continue;
}
if (typeof value === "object" && !Array.isArray(value) && value) {
result[key] = deepMergeDefaults(
value as typeof defaultObj,
passedObj[key as keyof typeof passedObj] as typeof defaultObj
);
continue;
}
result[key] = passedObj[key];
}
return result as T;
};
export const deepMergeDefaults = <T>(
defaultObj: {
[k: string]: unknown;
},
passedObj: { [k: string]: unknown }
) => {
let result: typeof defaultObj = {};
for (let [key, value] of Object.entries(defaultObj)) {
if (!passedObj[key as keyof typeof passedObj]) {
result[key] = value;
continue;
}
if (typeof value === "object" && !Array.isArray(value) && value) {
result[key] = deepMergeDefaults(
value as typeof defaultObj,
passedObj[key as keyof typeof passedObj] as typeof defaultObj
);
continue;
}
result[key] = passedObj[key];
}
return result as T;
};
type ContainerCustom = {
position?: {
left?: keyof typeof CONTAINER_PROPS["position"]["left"];
top?: keyof typeof CONTAINER_PROPS["position"]["top"];
};
padding?: typeof CONTAINER_PROPS["padding"];
};
type ContainerCustom = {
position?: {
left?: keyof typeof CONTAINER_PROPS["position"]["left"];
top?: keyof typeof CONTAINER_PROPS["position"]["top"];
};
padding?: typeof CONTAINER_PROPS["padding"];
};
type ContainerCustom = {
position: {
left: keyof typeof CONTAINER_PROPS["position"]["left"];
top: keyof typeof CONTAINER_PROPS["position"]["top"];
};
padding: typeof CONTAINER_PROPS["padding"];
};
type ContainerCustom = {
position: {
left: keyof typeof CONTAINER_PROPS["position"]["left"];
top: keyof typeof CONTAINER_PROPS["position"]["top"];
};
padding: typeof CONTAINER_PROPS["padding"];
};
0 Replies