paldepind / union-type

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

Type.ListOf #32

Open kwijibo opened 8 years ago

kwijibo commented 8 years ago

It would be useful to be able to define that a property of a type should be a typed list, eg:

const Shape = Type({Shape: [ListOf(Point)]})

Implementation could be something like (assuming the prototype version in the next branch)

Type.ListOf = type => list => R.all(R.is(type), list)
paldepind commented 8 years ago

Great idea. union-type allows one to use any validator function. So it should be possible to just define ListOf and use it like this

var ListOf = type => list => R.all(R.is(type), list)
const Shape = Type({Shape: [ListOf(Point)]})
kwijibo commented 8 years ago

Unfortunately this still doesn't work: TypeError: Expecting a function in instanceof check, but got #<Object> https://tonicdev.com/56d99860c541640d00a08d12/56e9228c007e791100e3d56a

kwijibo commented 8 years ago

Is ListOf a common enough modelling requirement that it would be worth adding the function to the library?

jgoux commented 8 years ago

I'd be very interested in this type too ! :+1: