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

string index signature limitation

Open danielnixon opened this issue 4 years ago • 1 comments

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.

danielnixon avatar Feb 04 '20 04:02 danielnixon

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.

juusaw avatar May 13 '20 12:05 juusaw