TypeScript's type inference can sometimes struggle with complex generic constraints, especially w...
Hello, I just tried to upgrade our repo from effect v3.10.0 to v3.15.0.
I have a problem with schema types. I have this function that gets the string from storage and parses it as a json (into provided schema).
const getParsed = <T extends Schema.Schema<any, any, unknown>>( key: string, schema: T ): Either.Either< Schema.Schema.Type<T>, ValueNotSet | ReadingFromStoreError | ParseResult.ParseError > => get(key).pipe(Either.flatMap(Schema.decodeEither( Schema.parseJson(schema) // <---- here is the TSerror )))
const getParsed = <T extends Schema.Schema<any, any, unknown>>( key: string, schema: T ): Either.Either< Schema.Schema.Type<T>, ValueNotSet | ReadingFromStoreError | ParseResult.ParseError > => get(key).pipe(Either.flatMap(Schema.decodeEither( Schema.parseJson(schema) // <---- here is the TSerror )))
Error being:
Argument of type 'transform<SchemaClass<unknown, string, never>, T>' is not assignable to parameter of type 'Schema<Type<T>, string, never>'. Types of property 'Context' are incompatible. Type 'Context<T>' is not assignable to type 'never'. Type 'unknown' is not assignable to type 'never'
Argument of type 'transform<SchemaClass<unknown, string, never>, T>' is not assignable to parameter of type 'Schema<Type<T>, string, never>'. Types of property 'Context' are incompatible. Type 'Context<T>' is not assignable to type 'never'. Type 'unknown' is not assignable to type 'never'
It seems like tyescript is unable to infer proper types when parsing (
T extends Schema.Schema<any, any, unknown>
T extends Schema.Schema<any, any, unknown>
type can not be passed as an argument to
Schema.parseJson
Schema.parseJson
).
It worked fine in 3.10.0. I think the problem might be in how I define the T parameter in the code (and that now there is
transform
transform
function in the
toJson
toJson
definition.
What would be the proper way how to define that function? I just need it to accept a schema that it tries to decode from jsonString...