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

Handle recursive types? #13

Open
pillowsoft opened this issue Oct 17, 2015 · 3 comments
Open

Handle recursive types? #13

pillowsoft opened this issue Oct 17, 2015 · 3 comments

Comments

@pillowsoft
Copy link

Is there any way to handle recursive types? I.e.

import {Record, List} from 'typed-immutable';

var Node = Record({
    data: String,
    subNodes: List(Node)
});

One can't do the above, as Node is not defined yet, when defining subNodes. Is there a way to accomplish this now, or do you have plans for this capability?

One possibility would be to add a constant, say "CurrentType", that would tell Record to use it's type for that member when creating new records. I.e.:

import {Record, List, CurrentType} from 'typed-immutable';
var Node = Record({
    data: String,
    subNodes: List(CurrentType)
});

Thoughts?

@angelf
Copy link

angelf commented Dec 19, 2015

I think it'd be best to allow the schema parameter to be a callback to the actual types.

As in..

import {Record, List} from 'typed-immutable';
var Node = Record(function() { return {
    data: String,
    subNodes: List(Node)
}})

@Gozala
Copy link
Collaborator

Gozala commented Jun 23, 2016

Unfortunately I am no longer able to maintain this project, I apologize. If someone wishes to step up and take leadership of this project please respond to #21

@angeloashmore
Copy link

I agree this should be doable (although I'm not sure what change are needed), but my workaround is to create a custom Typed. Since a custom Typed hides its type checking behind a function, the type checking occurs after the class/type definition.

Typed.Component = Typed('Component', value =>
  value instanceof Component ? value
                             : TypeError(`"${value}" is not a Component`))

export default class Component extends Record({
  name: String,
  components: List(Typed.Component),
  properties: List(Property)
}) {

// ...

}

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

4 participants