io-ts icon indicating copy to clipboard operation
io-ts copied to clipboard

DeriveClass from io-ts type mixin - looking for feedback / suggestions

Open jgreene opened this issue 7 years ago • 1 comments
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;

jgreene avatar Aug 26 '18 16:08 jgreene

Is it work for io-ts v2?

kirit0s avatar Jan 19 '21 10:01 kirit0s