Closed SAnDAnGE closed 5 years ago
Not sure about the cause
@SAnDAnGE UUID
is a brand codec, its type is
t.Type<UUID, string, unknown>
so the output type (string
) is different from the resulting type (UUID
), a codec base on UUID
must account for that
import * as t from 'io-ts'
import { UUID } from 'io-ts-types/lib/UUID'
export interface Category {
id: UUID
children: Array<Category>
}
export interface CategoryOutput {
id: string // <= !
children: Array<CategoryOutput>
}
export const Category: t.RecursiveType<t.Type<Category, CategoryOutput>> = t.recursion(
'Category',
() =>
t.type(
{
id: UUID,
children: t.array(Category)
},
'Category'
)
)
Thank you for the quick answer @gcanti !
Is there any way to make this work with io-ts-codegen... or I would have to stop using it for branded codecs ?
@SAnDAnGE https://github.com/gcanti/io-ts-codegen/pull/41
Could you please try it out by running npm i gcanti/io-ts-codegen#0.3.4-lib
?
Issue seems fixed for recursion, thank you for the quick fix and thank you for the awesome work you are doing !
I use io-ts-codegen to generate my decoders (helps a lot with recursion types). I used this:
to generate the following code:
I get this error for the Category const:
The code above is generated using io-ts-codegen. I made a stackblitz project to show this error and explain it better:
https://stackblitz.com/edit/typescript-nzgxxh
Not sure about the cause or what to do and where to fix it.