Skip to content

Commit 3308621

Browse files
fix(types): relax variant behavior on incompatible props
`variant` blocking on "incompatible props" use-case is bad for DX it only protects against bad spreads, which are antipattern anyway
1 parent 56e8e0d commit 3308621

File tree

2 files changed

+4
-6
lines changed

2 files changed

+4
-6
lines changed

public-types/reflect.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,9 @@ export function list<
206206
* ```
207207
*/
208208
export function variant<
209-
Props,
210209
CaseType extends string,
210+
Cases extends Record<CaseType, ComponentType<any>>,
211+
Props extends ComponentProps<Cases[CaseType]>,
211212
// It is ok here - it fixed bunch of type inference issues, when `bind` is not provided
212213
// but it is not clear why it works this way - Record<string, never> or any option other than `{}` doesn't work
213214
// eslint-disable-next-line @typescript-eslint/ban-types
@@ -216,7 +217,7 @@ export function variant<
216217
config:
217218
| {
218219
source: Store<CaseType>;
219-
cases: Partial<Record<CaseType, ComponentType<Props>>>;
220+
cases: Partial<Cases>;
220221
default?: ComponentType<Props>;
221222
bind?: Bind;
222223
hooks?: Hooks<Props>;

type-tests/types-variant.tsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { expectType } from 'tsd';
3434
expectType<React.FC>(VariableInput);
3535
}
3636

37-
// variant catches incompatible props between cases
37+
// variant allows to pass incompatible props between cases - resulting component will have union of all props from all cases
3838
{
3939
const Input: React.FC<{
4040
value: string;
@@ -56,7 +56,6 @@ import { expectType } from 'tsd';
5656
},
5757
cases: {
5858
input: Input,
59-
// @ts-expect-error
6059
datetime: DateTime,
6160
},
6261
});
@@ -264,7 +263,6 @@ import { expectType } from 'tsd';
264263
},
265264
cases: {
266265
button: Button<'button'>,
267-
// @ts-expect-error
268266
a: Button<'a'>,
269267
},
270268
});
@@ -277,7 +275,6 @@ import { expectType } from 'tsd';
277275
},
278276
cases: {
279277
button: Button<'button'>,
280-
// @ts-expect-error
281278
a: Button<'a'>,
282279
},
283280
});

0 commit comments

Comments
 (0)