Io-ts interface for properties with unknown keys
I think the answer you're looking for is record:
const myInterfaceCodec = t.record(t.string, t.union([t.string, t.undefined, t.null]));export type MyInterface = t.TypeOf<typeof myInterfaceCodec>;
=> type MyInterface = { [x: string]: string | null | undefined; }
Your use case:
const myInterfaceV = t.record(t.string, t.union([t.string, t.undefined, t.null]));export type MyInterface = t.TypeOf<typeof myInterfaceV>;const myOtherInterfaceV = t.intersection([ t.type({ requiredProp1: t.string, requiredProp2: t.string }), myInterfaceV]);export type MyOtherInterface = t.TypeOf<typeof myOtherInterfaceV>;const a: MyOtherInterface = { requiredProp1: "string", requiredProp2: "string2"};const b: MyOtherInterface = { requiredProp1: "string", requiredProp2: "string2", optionalProp1: "hello", optionalProp2: "world"};
Probably the most close to myInterface
in io-ts is t.UnknownRecord
export const MyOtherInterfaceV = t.interface({ requiredProp1: t.string, requiredProp2: t.string})const MyOtherInterface = t.intersection([ t.UnknownRecord, MyOtherInterfaceV ]);