io-ts
io-ts copied to clipboard
[Schema] extracting static types from extended Schema
🚀 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, useMySchema.TypeOf
instead ofSchema.TypeOf
- maybe
Schema.TypeOf
can betype 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 |