paldepind / union-type

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

Add simplified means of expressing records and newtypes #43

Open pelotom opened 8 years ago

pelotom commented 8 years ago

Type.New lets you make a type which is isomorphic to, but distinct from another type:

var ID = Type.New(String)

Type.Record lets you make a type with a single constructor which you don't have to name:

var Person = Type.Record({
  id: ID,
  name: String,
  age: Number
})

Use .from() to make instances of a Type.Record or Type.New:

var fred = Person.from({
  id: ID.from('2ialp7b4lu'), // passing a simple string here would of course be an error
  name: 'Fred',
  age: 24
})

To extract the wrapped value of a newtype, use .value:

console.log(fred.id.value) // prints '2ialp7b4lu'
davidchambers commented 8 years ago

These additions seem useful to me!

rickmed commented 8 years ago

me too!