Skip to content

Commit ef22274

Browse files
Sprinkles: Always return ConditionalValue type from utils (#159)
1 parent ac3299d commit ef22274

File tree

3 files changed

+17
-65
lines changed

3 files changed

+17
-65
lines changed

.changeset/afraid-ants-joke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/sprinkles': patch
3+
---
4+
5+
Always return `ConditionalValue` type from `normalizeValue` and `mapValue` functions

packages/sprinkles/src/createUtils.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,9 @@ export type RequiredConditionalValue<
7878

7979
export function createNormalizeValueFn<AtomicStyles extends Conditions<string>>(
8080
atomicStyles: AtomicStyles,
81-
): <Value extends ConditionalValue<AtomicStyles, string | number>>(
82-
value: Value,
83-
) => Value extends RequiredConditionalValue<AtomicStyles, infer PrimitiveValue>
84-
? RequiredConditionalObject<
85-
Exclude<ExtractDefaultCondition<AtomicStyles>, false>,
86-
Exclude<
87-
ExtractConditionNames<AtomicStyles>,
88-
Exclude<ExtractDefaultCondition<AtomicStyles>, false>
89-
>,
90-
PrimitiveValue
91-
>
92-
: Value extends ConditionalValue<AtomicStyles, infer PrimitiveValue>
93-
? Partial<Record<ExtractConditionNames<AtomicStyles>, PrimitiveValue>>
94-
: never {
81+
): <Value extends string | number>(
82+
value: ConditionalValue<AtomicStyles, Value>,
83+
) => Partial<Record<ExtractConditionNames<AtomicStyles>, Value>> {
9584
const { conditions } = atomicStyles;
9685

9786
if (!conditions) {
@@ -146,15 +135,6 @@ export function createMapValueFn<AtomicStyles extends Conditions<string>>(
146135
) => OutputValue,
147136
) => Value extends string | number
148137
? OutputValue
149-
: Value extends RequiredConditionalValue<AtomicStyles, string | number>
150-
? RequiredConditionalObject<
151-
Exclude<ExtractDefaultCondition<AtomicStyles>, false>,
152-
Exclude<
153-
ExtractConditionNames<AtomicStyles>,
154-
Exclude<ExtractDefaultCondition<AtomicStyles>, false>
155-
>,
156-
OutputValue
157-
>
158138
: Partial<Record<ExtractConditionNames<AtomicStyles>, OutputValue>> {
159139
const { conditions } = atomicStyles;
160140

tests/sprinkles/spinkles-type-tests.ts

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ import {
2020
// @ts-expect-error Unused args
2121
const noop = (...args: Array<any>) => {};
2222

23-
// @ts-expect-error Unused args
24-
const isString = (arg: string) => {};
25-
26-
// @ts-expect-error Unused args
27-
const isNumber = (arg: number) => {};
28-
2923
() => {
3024
const atoms = createAtomsFn(
3125
atomicWithShorthandStyles,
@@ -134,50 +128,26 @@ const isNumber = (arg: number) => {};
134128
// @ts-expect-error - Too many responsive array options
135129
normalizeValue(['one', 'two', 'three', 'four']);
136130

137-
// Valid value - default condition is provided
138-
isString(normalizeValue({ mobile: 'hello' }).mobile);
139-
140-
// Valid value - default condition is provided
141-
isString(normalizeValue({ mobile: 'hello', tablet: 'world' }).mobile);
142-
143-
// @ts-expect-error - condition is potentially undefined when not default condition
144-
isString(normalizeValue({ tablet: 'hello' }).tablet);
145-
146-
// @ts-expect-error - condition is potentially undefined when not default condition
147-
isString(normalizeValue({ mobile: 'hello', tablet: 'world' }).tablet);
148-
149131
normalizeValue({
150132
// @ts-expect-error - Incorrect conditional keys
151133
mobille: '',
152134
});
153135

136+
function testGenericNormalizeValue<Key extends string | number>(
137+
value: ResponsiveValue<Key>,
138+
): Key | undefined {
139+
const normalizedValue = normalizeValue(value);
140+
// Should return the Key type for each condition when normalizing
141+
return normalizedValue.mobile;
142+
}
143+
testGenericNormalizeValue('');
144+
154145
// @ts-expect-error - Strings shouldn't map to objects
155146
mapValue(alignProp, () => 'baz').mobile;
156147

157148
// @ts-expect-error - Numbers shouldn't map to objects
158149
mapValue(3, () => 4).mobile;
159150

160-
isString(mapValue({ mobile: 123 }, () => 'abc').mobile);
161-
isNumber(mapValue({ mobile: 'abc' }, () => 123).mobile);
162-
163-
// @ts-expect-error - Mobile should be potentially undefined since it wasn't provided
164-
isString(mapValue({ tablet: 123 }, () => 'abc').mobile);
165-
166-
// @ts-expect-error - Tablet should be potentially undefined since it's not the default condition
167-
isString(mapValue({ tablet: 123 }, () => 'abc').tablet);
168-
169-
// @ts-expect-error - Tablet should be potentially undefined since it's not the default condition
170-
isString(mapValue({ mobile: 123, tablet: 456 }, () => 'abc').tablet);
171-
172-
// @ts-expect-error - Mobile should be potentially undefined since it wasn't provided
173-
isNumber(mapValue({ tablet: 'abc' }, () => 123).mobile);
174-
175-
// @ts-expect-error - Tablet should be potentially undefined since it's not the default condition
176-
isNumber(mapValue({ tablet: 'abc' }, () => 123).tablet);
177-
178-
// @ts-expect-error - Tablet should be potentially undefined since it's not the default condition
179-
isNumber(mapValue({ mobile: 'abc', tablet: 'def' }, () => 123).tablet);
180-
181151
const mapValueWithoutDefaultCondition = createMapValueFn(
182152
conditionalStylesWithoutDefaultCondition,
183153
);
@@ -188,9 +158,6 @@ const isNumber = (arg: number) => {};
188158
// @ts-expect-error - Should force conditional value as no default condition
189159
normalizeValueWithoutDefaultCondition('test');
190160

191-
// @ts-expect-error - Should potentially be undefined
192-
isString(normalizeValueWithoutDefaultCondition({ active: 'yo' }).active);
193-
194161
// @ts-expect-error - Should force conditional value as no default condition
195162
mapValueWithoutDefaultCondition('test');
196163

0 commit comments

Comments
 (0)