diff --git a/.changeset/dirty-brooms-vanish.md b/.changeset/dirty-brooms-vanish.md new file mode 100644 index 000000000..b9baecc91 --- /dev/null +++ b/.changeset/dirty-brooms-vanish.md @@ -0,0 +1,6 @@ +--- +'@vanilla-extract/private': patch +'@vanilla-extract/css': patch +--- + +Fix `createThemeVars` when using null values diff --git a/packages/private/src/walkObject.ts b/packages/private/src/walkObject.ts index faf765dc2..09983953d 100644 --- a/packages/private/src/walkObject.ts +++ b/packages/private/src/walkObject.ts @@ -1,26 +1,37 @@ import { MapLeafNodes } from './types'; -export function walkObject( +type Primitive = string | number | null | undefined; + +type Walkable = { + [Key in string | number]: Primitive | Walkable; +}; + +export function walkObject( obj: T, - fn: (value: string | number, path: Array) => MapTo, + fn: (value: Primitive, path: Array) => MapTo, path: Array = [], ): MapLeafNodes { - // @ts-expect-error const clone = obj.constructor(); for (let key in obj) { const value = obj[key]; const currentPath = [...path, key]; - if (typeof value === 'object') { - clone[key] = value ? walkObject(value, fn, currentPath) : value; - } else if (typeof value === 'string' || typeof value === 'number') { - clone[key] = fn(value, currentPath); + if ( + typeof value === 'string' || + typeof value === 'number' || + value == null + ) { + clone[key] = fn(value as Primitive, currentPath); + } else if (typeof value === 'object' && !Array.isArray(value)) { + clone[key] = walkObject(value as Walkable, fn, currentPath); } else { console.warn( `Skipping invalid key "${currentPath.join( '.', - )}". Should be a string, number or object. Received: "${typeof value}"`, + )}". Should be a string, number, null or object. Received: "${ + Array.isArray(value) ? 'Array' : typeof value + }"`, ); } }