io-ts
io-ts copied to clipboard
DeriveClass from io-ts type mixin - looking for feedback / suggestions
trafficstars
First of all, great library!
I've created a proof of concept for deriving classes from io-ts types using a mixin. You can view it here: https://github.com/jgreene/io-ts-derive-class
My use case is the ability to use libraries that associate metadata with a class constructor while still being able to reflect over the type structure.
Please let me know if you have any feedback.
Here is a quick example of what it does:
const CityType = t.type({
ID: t.number,
Name: t.string
})
class City extends m.DeriveClass(CityType) {}
const AddressType = t.type({
StreetAddress1: t.string,
StreetAddress2: t.string,
City: m.ref(City)
});
class Address extends m.DeriveClass(AddressType) {}
const PersonType = t.type({
ID: t.number,
FirstName: t.string,
LastName: t.string,
Address: m.ref(Address)
});
class Person extends m.DeriveClass(PersonType) {}
which gives the model one would expect when using it.
let person = new Person({ FirstName: 'Test', LastName: 'TestLast'});
let cityName = person.Address.City.Name;
Is it work for io-ts v2?