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
The current behavior of Validate<I, A> is to effectively make I = unknown 100% of the time. Consider the following example
import{Temporal}from'@js-temporal/polyfill';import*astfrom'io-ts';constTemporalInstant=newt.Type<Temporal.Instant,string,bigint|string|Temporal.Instant>('Instant',(v): v is Temporal.Instant=>vinstanceofTemporal.Instant,(v: unknown,c)=>{// the parameter v, here, should be unknown (instead of I, aka bigint | string | Temporal.Instant)try{constresult=newTemporal.Instant(v);returnt.success(result);}catch(e){returnt.failure(v,c);}},(v)=>v.toString(),);constWidget=t.type({name: t.string,/* TS2322: Type 'Type<Instant, string, string | bigint | Instant>' is not assignable to type 'Mixed'. Types of property 'validate' are incompatible. Type 'Validate<string | bigint | Instant, Instant>' is not assignable to type 'Validate<unknown, any>'. Type 'unknown' is not assignable to type 'string | bigint | Instant'. */createdAt: TemporalInstant,});
Desired Behavior
Understandibly the incoming data is always unknown - so in a way, that doesn't actually need to be a generic type.
What is important is signaling to consumers of the type what types of inputs are expected. Put another way, InputOf<T> could be used as a type that consumers could actually make use of.
// giventypeWidgetInput=t.InputOf<typeofWidget>;// what actually happenstypeCurrentBehavior=unknown;// what could happen in a perfect worldtypeExpectedBehavior={name: string,createdAt: string|bigint|Temporal.Instant,}
Suggested Solution
This might be as simple as changing the interface for Decoder:
Since I = unknown is the only case that exists, this shouldn't have much of an effect on the behavior of anything. Then it would be necessary to modify all of the existing types to make use of the new generic arguments, ie:
export class StringType extends Type<string> {}
Should be changed to
export class StringType extends Type<string, string, string> {}
Who does this impact? Who is this for?
Anyone who wants to use InputType<T> to model valid inputs for consumers of API's backed by io-ts.
Describe alternatives you've considered
Using covariance and contravariance hacks, subclassing Type in my own project.
Your environment
Software
Version(s)
io-ts
2.2.17
fp-ts
TypeScript
4.7.2
The text was updated successfully, but these errors were encountered:
🚀 Feature request
Current Behavior
The current behavior of
Validate<I, A>
is to effectively makeI = unknown
100% of the time. Consider the following exampleDesired Behavior
Understandibly the incoming data is always unknown - so in a way, that doesn't actually need to be a generic type.
What is important is signaling to consumers of the type what types of inputs are expected. Put another way,
InputOf<T>
could be used as a type that consumers could actually make use of.Suggested Solution
This might be as simple as changing the interface for
Decoder
:Since
I = unknown
is the only case that exists, this shouldn't have much of an effect on the behavior of anything. Then it would be necessary to modify all of the existing types to make use of the new generic arguments, ie:Should be changed to
Who does this impact? Who is this for?
Anyone who wants to use
InputType<T>
to model valid inputs for consumers of API's backed by io-ts.Describe alternatives you've considered
Using covariance and contravariance hacks, subclassing
Type
in my own project.Your environment
The text was updated successfully, but these errors were encountered: