import * as t from 'io-ts';
import * as tt from 'io-ts-types';
const Entity = t.type({
id: t.string,
});
type Entity = t.TypeOf<typeof Entity>;
const addEntityRequest = <E extends t.Type<Entity>>(entity: E) =>
t.type({
entity,
});
const User = t.intersection([
Entity,
t.type({
name: t.string,
}),
]);
type User = t.TypeOf<typeof User>;
const AddUserRequest = addEntityRequest(User);
type AddUserRequest = t.TypeOf<typeof AddUserRequest>;
but this line is giving me an error where I pass the User codec into addEntityRequest
const AddUserRequest = addEntityRequest(User);
Argument of type 'IntersectionC<[TypeC<{ id: StringC; }>, TypeC<{ name: StringC; }>]>' is not assignable to parameter of type 'Type<{ id: string; }, { id: string; }, unknown>'.
Types of property 'encode' are incompatible.
Type 'Encode<{ id: string; } & { name: string; }, { id: string; } & { name: string; }>' is not assignable to type 'Encode<{ id: string; }, { id: string; }>'.
Type '{ id: string; }' is not assignable to type '{ id: string; } & { name: string; }'.
Property 'name' is missing in type '{ id: string; }' but required in type '{ name: string; }'.ts(2345)
I can only get it to work if I change the generic constraint on addEntityRequest to t.Mixed
But then this won't enforce the passed in codec be an Entity codec. I think I must be missing something when defining the generic constraint, but can't seem to figure it out. Any help would be greatly appreciated, thanks!
I'm trying to make codecs for the following types
So I tried this (based on the guidelines in the documentation https://github.com/gcanti/io-ts/blob/master/index.md#generic-types)
but this line is giving me an error where I pass the User codec into addEntityRequest
I can only get it to work if I change the generic constraint on addEntityRequest to t.Mixed
But then this won't enforce the passed in codec be an Entity codec. I think I must be missing something when defining the generic constraint, but can't seem to figure it out. Any help would be greatly appreciated, thanks!