gcanti / io-ts

Runtime type system for IO decoding/encoding
https://gcanti.github.io/io-ts/
MIT License
6.7k stars 328 forks source link

Mapped types in experimental API #623

Open florianbepunkt opened 2 years ago

florianbepunkt commented 2 years ago

📖 Documentation

It would be great if the documentation would contain a mapping between methods of the stable and experimental APIs. This would help to translate prior knowledge / work to the new API.

My current case: Given this code from #601 from @lingard How would this translate to the experimental API? I have a use case (filter DSL) that would benefit from this

import * as t from 'io-ts'

const stringify = <T extends t.Props>(type: t.TypeC<T>) =>
  t.record(t.keyof(type.props), t.string)

const config = t.type({
  hostname: t.string,
  port: t.number
})

const stringified = stringify(config)

type Stringified = t.TypeOf<typeof stringified>

// type Stringified = {
//   hostname: string;
//   port: string;
// }

My use case

type Filter<A> = {field: keyof A; value: O.Option<A[keyof A]>}

// How to translate the concept of keyof to io-ts (experimental API)?