Skip to content

Commit e154028

Browse files
authored
Fix createThemeVars when using null values (#45)
1 parent ccd7c58 commit e154028

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

.changeset/dirty-brooms-vanish.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@vanilla-extract/private': patch
3+
'@vanilla-extract/css': patch
4+
---
5+
6+
Fix `createThemeVars` when using null values

packages/private/src/walkObject.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
import { MapLeafNodes } from './types';
22

3-
export function walkObject<T, MapTo>(
3+
type Primitive = string | number | null | undefined;
4+
5+
type Walkable = {
6+
[Key in string | number]: Primitive | Walkable;
7+
};
8+
9+
export function walkObject<T extends Walkable, MapTo>(
410
obj: T,
5-
fn: (value: string | number, path: Array<string>) => MapTo,
11+
fn: (value: Primitive, path: Array<string>) => MapTo,
612
path: Array<string> = [],
713
): MapLeafNodes<T, MapTo> {
8-
// @ts-expect-error
914
const clone = obj.constructor();
1015

1116
for (let key in obj) {
1217
const value = obj[key];
1318
const currentPath = [...path, key];
1419

15-
if (typeof value === 'object') {
16-
clone[key] = value ? walkObject(value, fn, currentPath) : value;
17-
} else if (typeof value === 'string' || typeof value === 'number') {
18-
clone[key] = fn(value, currentPath);
20+
if (
21+
typeof value === 'string' ||
22+
typeof value === 'number' ||
23+
value == null
24+
) {
25+
clone[key] = fn(value as Primitive, currentPath);
26+
} else if (typeof value === 'object' && !Array.isArray(value)) {
27+
clone[key] = walkObject(value as Walkable, fn, currentPath);
1928
} else {
2029
console.warn(
2130
`Skipping invalid key "${currentPath.join(
2231
'.',
23-
)}". Should be a string, number or object. Received: "${typeof value}"`,
32+
)}". Should be a string, number, null or object. Received: "${
33+
Array.isArray(value) ? 'Array' : typeof value
34+
}"`,
2435
);
2536
}
2637
}

0 commit comments

Comments
 (0)