Avoid type widening with enum-typed values in FormValue#541
Avoid type widening with enum-typed values in FormValue#541tmcw wants to merge 1 commit intoedmundhung:mainfrom
Conversation
|
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. |
The current generic type for
FormValueis this:In this, if you have an enum or constant string type, then it will get widened into a
stringtype. TurningDateandnulltypes 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 theFormValuegeneric uses that type instead of widening to string.