You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Basically, we don't recommend to use our Result type in ser/de format directly.
We recommend:
Use some framework library hide them in their magical veil.
Intoduce some scheme and ser/de specific mechanism and you should write a layer to recover from a deserialized value and bridge to more "solid" typing world.
However, especially in web frontend or other small scripts, you sometimes do not setup data exchange specific type & format, and use serializable value directly, via JSON.stringify and JSON.parse ugh.
For example, implement "hydration" mechanism by self without taking a framework. To build a their ones for their application.
In such case, as poor man's design (or skip overhead in hydration steps), they want to do minimum value runtime type checking as type guard without any other existing serialize protocol. So they just implement their own data exchange protocol!
This issue discuss about that a type guard function which we should as default (or should not do).
The text was updated successfully, but these errors were encountered:
This an example to type guard function for PlainResult/Result.
But we already have a some discussion points:
How level do you check an actual object deeply? The following code is just returns Result<unknown, unknown>, but it useless. This shows more additional checks to get more concrete type.
Which version of this package do you assume about a value?option-t/PlainResult/Result has a bit legacy variants (e.g. Todays' result.val === null if it's created by createOk(). But some legacy version would be result.val === undefined).
Should we change the Result type definition for them purpose? As a part of code cleanup, I would like to make Result type more simply but it might break a backward compatibility of types. It would be problematic for large scale codebase mixing a multiple versions of libaries and many versions of option-t also lives. For them, we need to investigate a way to avoid to break a backward compatibility.
importtype{Result}from'./result.js';functionhasProp<KextendsPropertyKey>(data: object,prop: K): data is Record<K,unknown>{returnpropindata;}exportfunctionisResultType(value: unknown): value is Result<unknown,unknown>{if(typeofvalue!=='object'){returnfalse;}if(!value){returnfalse;}if(!hasProp(value,'ok')){returnfalse;}if(typeofvalue.ok!=='boolean'){returnfalse;}if(value.ok){if(!hasProp(value,'val')){returnfalse;}}elseif(!hasProp(value,'err')){returnfalse;}returntrue;}``
Motivation
Basically, we don't recommend to use our Result type in ser/de format directly.
We recommend:
However, especially in web frontend or other small scripts, you sometimes do not setup data exchange specific type & format, and use serializable value directly, via
JSON.stringify
andJSON.parse
ugh.For example, implement "hydration" mechanism by self without taking a framework. To build a their ones for their application.
In such case, as poor man's design (or skip overhead in hydration steps), they want to do minimum value runtime type checking as type guard without any other existing serialize protocol. So they just implement their own data exchange protocol!
This issue discuss about that a type guard function which we should as default (or should not do).
The text was updated successfully, but these errors were encountered: