Effect CommunityEC
Effect Community3y ago
10 replies
spaethnl

Typescript's Limitations on Circular Use of `infer` in Schema

While debugging some issues I've been having with circular types, I discovered that the problem is largely related to typescript's limits regarding circular use of infer. Schema really only uses infer in a couple of places: From, To are the biggest culprits because they infer the types from functions using ReturnType, which uses infer under the hood. I found that if I patch Schema to:
export interface Schema<From, To = From> extends Pipeable {
  readonly toType: To
  readonly fromType: From
  //.... rest of current properties
}

Then you can redefine To/From to:
export type From<S extends Schema<any,any>> = S['fromType']
export type To<S extends Schema<any,any>> = S['toType']

And do a similar thing to Class, then suddenly my recursive types work as expected.
Is there any reason not to do this?
Was this page helpful?