Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tag recursion #80

Closed
sledorze opened this issue Oct 15, 2017 · 2 comments
Closed

tag recursion #80

sledorze opened this issue Oct 15, 2017 · 2 comments

Comments

@sledorze
Copy link
Collaborator

sledorze commented Oct 15, 2017

I'm currently writing a lib to generate Generators for 'testcheck' (https://github.com/leebyron/testcheck-js) library (btw any constructive criticism on that choice is welcome)

I've covered so far:

interface, string, number, boolean, literal, union, intersection (of recursive unions/intersections of interfaces/partials), array, partial
I can't implement recursion because io-ts currently does not provide a tag on it.
Can it be done?

Thanks!

@gcanti
Copy link
Owner

gcanti commented Oct 16, 2017

I'm currently writing a lib to generate Generators for 'testcheck'

That's a great idea

I can't implement recursion because io-ts currently does not provide a tag on it

What do you need in order to implement recursion?

In the 80 branch you can find a POC (added a RecursiveType type) and you can introspect like this

type ICategory = {
  name: string
  categories: Array<ICategory>
}

const Category = t.recursion('Category', self =>
  t.interface({
    name: t.string,
    categories: t.array(self)
  })
)

console.log(Category._tag) // RecursiveType
console.log(Category.name) // Category
console.log((Category.type as any)._tag) // InterfaceType
const type = Category.type as t.InterfaceType<any>
console.log(type.props.categories.type) // RecursiveType { name: 'Category', ... }

You can try it out with npm i gcanti/io-ts#80

@sledorze
Copy link
Collaborator Author

I've done a quick check (pun intended),
@gcanti that _tag information is all I need, may it be merged?

gcanti added a commit that referenced this issue Oct 16, 2017
gcanti added a commit that referenced this issue Oct 16, 2017
@gcanti gcanti closed this as completed in 8455e7e Oct 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants