import { createTheme } from '@vanilla-extract/css'; import type { Contract, MapLeafNodes } from '@vanilla-extract/private'; import { DeepPartial } from 'utility-types'; function getVarsIntersection< A extends Record, B extends Record, X extends Extract = Extract, >(a: A, b: B): Record { const result: Record = {}; for (const [key, bValue] of Object.entries(b)) { const aValue = a[key]; if (!aValue || !bValue) { continue; } if (typeof bValue === 'string' && typeof aValue === 'string') { result[key] = aValue; continue; } result[key] = getVarsIntersection(aValue, bValue); } return result as Record; } export function extendTheme( vars: ThemeContract, extendedVars: NonNullable>>, ) { const requiredVars = getVarsIntersection(vars, extendedVars); return createTheme( requiredVars as ThemeContract, extendedVars as MapLeafNodes, ); }