The class you want to construct an instance of
Properties you want to set on the instance
If you had the following User model in your application
class User {
id!: number;
name!: string;
get initials(): string {
const parts = this.name.split(' ');
return parts[0][0] + parts[1][0];
}
}
your instance decoder would look like this.
const UserDecoder = Decode.instance(User, {
id: Decode.field('id', Decode.integer),
name: Decode.field('name', Decode.string),
});
Running this decoder on raw values will ensure that you always have an actual instance of your User class at hand.
const kobe = decode(UserDecoder, { id: 3, name: 'Kobe Bryant' });
console.assert(kobe.id === 3)
console.assert(kobe.name === 'Kobe Bryant')
console.assert(kobe.initials === 'KB')
Generated using TypeDoc
This is mostly equivalent to Decode.object, but it creates an instance of the given class first. You should only use this for simple classes that don't have a complex constructor. Use
map
orandThen
for complicated cases.