ts-to-io
ts-to-io copied to clipboard
string index signature limitation
Given some type with non-optional properties:
export interface Address {
street_address: string;
city: string;
state: string;
}
we get the following io-ts codec:
const Address = t.type({street_address: t.string, city: t.string, state: t.string})
So good so far. However, if we add a string index signature:
export interface Address {
street_address: string;
city: string;
state: string;
[k: string]: any;
}
We now get the following io-ts codec:
const Address = t.record(t.string, t.unknown)
That suits the string index signature, but we've lost validation of the three required fields.
The generated codec could probably be improved by adding some extra cases to the type determining logic of ts-to-io. The project should ideally be able to generate something like
const Address = t.intersection(t.record(t.string, t.unknown), t.type({street_address: t.string, city: t.string, state: t.string}))
I'll try to look into it at some point.