Basic setup on how to have strict typing to make sure no mismatching values are ever passed to the schema:
import { Schema } from 'leva/dist/declarations/src/types';
type ControlFor<T> = {
value: T;
options: readonly T[];
};
type StrictSchema<C> = {
[K in keyof C]: ControlFor<C[K]>;
};
type Config = {
color: 'red' | 'blue' | 'green';
};
const my_schema: StrictSchema<Config> = {
color: { value: 'blue', options: ['red'] },
};
const C = () => {
const store = useCreateStore();
const { color } = useControls('settings', my_schema, { store });
return (
<>
<LevaPanel store={store} />
<div style={{ background: color }}>test</div>
</>
);
};
This approach enforces a relationship between value and options, but it requires defining custom wrapper types (ControlFor, StrictSchema) instead of using the built-in Schema and is still far from perfect for all possible values.
It would be useful if the library exposed a stricter, generic-friendly schema type (or helper) that preserves the link between:
- the control value
- and its allowed options
Right now, Schema[keyof Schema] is too permissive, so mismatches like invalid options are not caught by TypeScript without additional user-defined types.
Basic setup on how to have strict typing to make sure no mismatching values are ever passed to the schema:
This approach enforces a relationship between value and options, but it requires defining custom wrapper types (ControlFor, StrictSchema) instead of using the built-in Schema and is still far from perfect for all possible values.
It would be useful if the library exposed a stricter, generic-friendly schema type (or helper) that preserves the link between:
Right now,
Schema[keyof Schema]is too permissive, so mismatches like invalid options are not caught by TypeScript without additional user-defined types.