Closed Industrial closed 10 months ago
There are a few of ways to do this.
BigIntString
using fp-ts 'unsafeCoerce'. I use this a lot at work when I know what I'm coercing abides the underlying branded type.number
to Int
, number
to Float
, Date
to SafeDate
and string
to BigIntString
is castable with one as
clause.const guard = deriveInputGuard(S.BigIntString)
and use guard.is()
to instruct typescript that the type is safely the underlying type.Most of the time for branded types I know to be valid values, for example, Int
's, Float
s, SafeDate
's, etc I just use unsafeCoerce
because there is no concern about getting it wrong. If the source of the type is unknown, i.e. from a network request then you should first .decode()
from deriveTranscoder()
or .is()
from deriveGuard()
/ deriveInputGuard()
to ensure that the type matches the expected type.
Also, to clarify, casting and using unsafeCoerce
should only be done when you write the value of the type yourself (or as an escape hatch when you're certain the type coming in is trustworthy).
If the value is coming from a somewhere else, you should lift the function into an Either type and use Transcoders to decode the value first. This will guarantee type safety. And alternatively you can use guard in this context for the same effect.
Okay, thanks.
I have this in my Schema:
and get this error: