io-ts icon indicating copy to clipboard operation
io-ts copied to clipboard

[Schema] extracting static types from extended Schema

Open ninesunsabiu opened this issue 2 years ago • 0 comments

🚀 Feature request

Current Behavior

When I extended my Schema according to How to extend the built-in Schema, I couldn't use Schema.Typeof to get the types

import * as S from 'io-ts/Schema'

const PersonSchema = MySchema.make((S) =>
  S.struct({
    name: S.string,
    age: S.Int
  })
)

type Person = S.TypeOf<typeof PersonSchema>
//      ^ it is never

because Schemable in Schema is contravariant

Desired Behavior

type Person = S.TypeOf<typeof PersonSchema>

/*
type Person = {
    name: string;
    age: Int;
}
*/

Suggested Solution

  • in document, add a step: export type TypeOf<S> = S extends MySchema<infer A> ? A : never and when need extracting static types, use MySchema.TypeOf instead of Schema.TypeOf
  • maybe Schema.TypeOf can be type TypeOf<S> = S extends (i: any) => HKT<any, infer R> ? R : never. I don't known

Who does this impact? Who is this for?

All users

Describe alternatives you've considered

Additional context

Your environment

Software Version(s)
io-ts 2.2.16
fp-ts 2.11.8
TypeScript 4.7.0-dev.20220419

ninesunsabiu avatar May 09 '22 14:05 ninesunsabiu