Types like NumberFromString currently allow decoding from any type (unknown). This can be useful but also error prone. For example, we might accidentally pass in a value which is already a number. We won't get a type error because decode is asking for unknown, which number is assignable to.
NumberFromString.decode(100) // no type error, returns a Left
Expected behavior
Reproducible example
Suggested solution(s)
If we changed the input from unknown to string, then decode would only work with strings. This way, mistakes like the one above would result in helpful type errors at compile time:
NumberFromString.decode(100) // type error!
NumberFromString.decode('100') // no type error, returns a Right
š Bug report
Current Behavior
Types like
NumberFromString
currently allow decoding from any type (unknown
). This can be useful but also error prone. For example, we might accidentally pass in a value which is already a number. We won't get a type error becausedecode
is asking forunknown
, whichnumber
is assignable to.Expected behavior
Reproducible example
Suggested solution(s)
If we changed the input from
unknown
tostring
, thendecode
would only work withstring
s. This way, mistakes like the one above would result in helpful type errors at compile time:Implementation:
This is how I would expect the type to behave based on its name ("from string").
If the user needs to decode a value of type
unknown
, they can uset.string.pipe(NumberFromString)
instead ofNumberFromString
.WDYT? If you agree I will happily send a PR!
Additional context
Your environment
Which versions of io-ts-types are affected by this issue? Did this work in previous versions of io-ts-types?