• 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 or andThen for complicated cases.

    Type Parameters

    • O

    Parameters

    • ctor: (new () => O)

      The class you want to construct an instance of

        • new (): O
        • Returns O

    • layout: ObjectLayout<O>

      Properties you want to set on the instance

    Returns Decoder<O>

    Example

    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')

    See

    • object
    • ObjectLayout

Generated using TypeDoc