conform
conform copied to clipboard
Avoid type widening with enum-typed values in FormValue
The current generic type for FormValue
is this:
export type FormValue<Schema> = Schema extends
| string
| number
| boolean
| Date
| bigint
| null
| undefined
? string | undefined
In this, if you have an enum or constant string type, then it will get widened into a string
type. Turning Date
and null
types into string | undefined makes sense, but it is kind of just inconvenient to lose type information if you have an enum.
This tweaks the generic type so that if you have a type that extends string
, then the FormValue
generic uses that type instead of widening to string.
This is tricky... because conform does not validate the form value. The type you got is more or less a faith that the type should be mostly correct if you are using Conform properly. But it couldn't stop you from putting a hidden input with an invalid enum which will be reflected on form value regardless. 😅
Maybe it is alright to made the same assumption with enum here... What do you think?
I think that that's what typescript's about, constraining things to be something we want even though Javascript allows to do about anything. If you use typescript incorrectly or if you make a mistake and mistakenly widen a type to things you don't want to, you will fuck yourself up anyway :D
On the other hand since inputs are strings / files by design.. I would slightly incline to have it widened and put the responsibility on the developer. I know, I also don't like not having the convenience but.. It forces the developer to put more checks in place.
might be worth using the string interpolation type:
https://www.typescriptlang.org/play?#code/C4TwDgpgBAYg9gJwLYDUCGAbArhAPAZQGMALCJNAPigF4ojTyoIAPYCAOwBMBnAKCigAfKN2AIAluwDm-IVHZYkAIwgJZwpeKmTg6qErhwMENOz0KMGPVi4QAZpIidZAfigADACQBvemTQAvu5yNpz2js4CAFx0JP5MrBw8sOLGrinGIbYO7E6yMbkAbqoA3Ly8oJAZEOjY0LTwyLU4uDCpEBRlldD4YpJSzfWwiKiYLaIS0p0V4D190gCq7OJw7IM0w01jeABEAIw7cjsATDvT3VAAcooqCOsNI4O4Csqq57NXN6pLK2vbG41RnVcHs5MdprwAPSQqA2bhYBDQcR2KDAYjibhQADucCwGE4+mgLEghDYnAANPosMAoCBcVApBAaWjoNpRAA6GZVABCWgAkuxgPdNkCWpptIL3jz+YKfqthYCnnt2GD2FLoEswjknArHttcKFwrlOOrPpZdVtgRYMNMgA