paldepind / union-type

A small JavaScript library for defining and using union types.
MIT License
478 stars 28 forks source link

Support lists of types natively or via an exposed predicate. #48

Closed JAForbes closed 6 years ago

JAForbes commented 8 years ago

Is there a way to make a type checked collection?

const Header = Type({
    Header: { 
        signature: String
        , byteOffset: Number
        , signatureName: String 
    }
})

const Model = Type({
        Model: { headers: R.all(Header.is) }
})

Header.is doesn't exist currently, but I'm imaging if a union had a predicate you could create things like R.both( R.isArray, R.all(Header.is) ) to specify that field should be a Header[].

If there is a simpler approach as well, that would be great! πŸ‘

I think it'd be nice to be able to specify a list of a given type as a built in behaviour as well.

E.g.

const Model = Type({
        // wrap a record in an array to signify Header[]
        Model: { headers: [Header] }
        // or for a non record type
        Model: [ [Header] ]
        // or dynamically add a property to signify Header[]
        Model: { headers: Header.List } 
        Model: [ Header.List ]
})

If we had access to the internal prototypes, we could use R.is, or a.isPrototypeOf(A), but I think the currying gets in the way of that.

paldepind commented 8 years ago

Hello. I've just done a release with #41. Does the feature in that PR do what you want?

JAForbes commented 8 years ago

Wow 😲 that's great.

I still think it would be nice if every union exposed a predicate. But I'm happy with this new feature. πŸ‘Œ

-----Original Message----- From: "Simon Friis Vindum" notifications@github.com Sent: β€Ž8/β€Ž9/β€Ž2016 8:54 PM To: "paldepind/union-type" union-type@noreply.github.com Cc: "James Forbes" james.a.forbes@gmail.com; "Author" author@noreply.github.com Subject: Re: [paldepind/union-type] Support lists of types natively or via anexposed predicate. (#48)

Hello. I've just done a release with #41. Does the feature in that PR do what you want? β€” You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.