I am working for validating api response by using io-ts. https://github.com/gcanti/io-ts
I have already defined following type of TypeScript.
export type Group = {
id: number
name: string
}
And I want to use this type in io-ts as below.
const UploadedSearch = t.type({
id: t.number,
title: t.string,
groups: t.array(Group),
})
But I got a following error.
TS2693: 'Group' only refers to a type, but is being used as a value here.
Is there any way to use Group in io-ts code ?
The comment on your question already mentioned this but to illustrate how to use io-ts
not to repeat yourself, you can say:
const GroupCodec = t.type({
id: t.number,
name: t.string,
});
// Will be equivalent to your group type, so no need to repeat yourself.
export type Group = t.TypeOf<typeof GroupCodec>;
const UploadedSearch = t.type({
id: t.number,
title: t.string,
groups: t.array(GroupCodec),
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.