Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Schema] extracting static types from extended Schema #646

Open
ninesunsabiu opened this issue May 9, 2022 · 0 comments
Open

[Schema] extracting static types from extended Schema #646

ninesunsabiu opened this issue May 9, 2022 · 0 comments

Comments

@ninesunsabiu
Copy link

ninesunsabiu commented May 9, 2022

馃殌 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant