Effect CommunityEC
Effect Community2y ago
10 replies
spaethnl

TypeScript Type Incompatibility Issue with Generic Schema and Optional Values

Is there a straight forward way of getting the following work? The goal is to make it easy to create new schemas where the value field varies.
I've tried various uses of asSchema() and AsSchema<> with no luck.
In this case, I could leave off the explicit return type, but the actual intent was to be able to use that type in other places and still have it be compatible.
type TypeWithValue<Value extends S.Schema.Any> = { value: S.optionalWithOptions<Value, { nullable: true; }> } // SomeOtherFields &
const makeTypeWithValue = <Value extends S.Schema.Any>(value: Value): TypeWithValue<Value> => ({
  //...SomeOtherFields
  value: S.optional({ nullable: true })(value)
})
// ts [2322]: Type 'optionalWithOptions<All, { nullable: true; }>' is not assignable to type 'optionalWithOptions<Value, { nullable: true; }>'.
//    Types of property 'from' are incompatible.
//       Type 'All' is not assignable to type 'Value'.
//         'Value' could be instantiated with an arbitrary type which could be unrelated to 'All'
Was this page helpful?