Io-ts: Unable to define partial record

Created on 6 May 2019  路  3Comments  路  Source: gcanti/io-ts

With io-ts 1.8.4 the following does not work.

type Stuff = {
  foo: string,
  bar: number,
};
const Stuff = t.type({
  foo: t.string,
  bar: t.number,
})

type Broken = Partial<Record<keyof Stuff, void>>;
const Broken = t.partial(t.record(t.keyof(Stuff), t.void).props);


const value: Broken = Broken.decode({
  foo: 'test',
}).getOrElseL(() => { throw 'error'});

What happens:

Type error: Property 'props' does not exist on type 'RecordC<KeyofC<TypeC<{ foo: StringC; bar: NumberC; }>>, VoidC>'.  TS2339

    31 |
    32 | type Broken = Partial<Record<keyof Stuff, void>>;
  > 33 | const Broken = t.partial(t.record(t.keyof(Stuff), t.void).props);
       |                                                           ^
    34 |
    35 |
    36 | const value: Broken = Broken.decode({

Most helpful comment

I worked around this be using t.record(t.union([..., t.undefined], ...) and wrapping the t.TypeOf with Partial. It would be nice if t.partial(t.record... was supported.

All 3 comments

Actually, even without the partial there is a problem. For example

type Stuff = {
  foo: string,
  bar: number,
};
const Stuff = t.type({
  foo: t.string,
  bar: t.number,
})

type Broken = Record<keyof Stuff, void>;
const Broken = t.record(t.keyof(Stuff), t.void);


const value: Broken = Broken.decode({
  foo: 'test',
}).getOrElseL(() => { throw 'error'});

What happens:

Type error: Type '{ _A: void; _O: void; name: void; props: void; _tag: void; is: void; validate: void; encode: void; _I: void; pipe: void; asDecoder: void; asEncoder: void; decode: void; }' is missing the following properties from type 'Record<"foo" | "bar", void>': foo, bar  TS2739

    34 |
    35 |
  > 36 | const value: Broken = Broken.decode({
       |       ^
    37 |   foo: 'test',
    38 | }).getOrElseL(() => { throw 'error'});

I'm working on a symmetric protocol where the server fills in the blanks found in request and returns a similar structure to frontend.

I worked around this be using t.record(t.union([..., t.undefined], ...) and wrapping the t.TypeOf with Partial. It would be nice if t.partial(t.record... was supported.

Was this page helpful?
0 / 5 - 0 ratings