How to pass a Variant through Candid

I have a variant nested within a record type

public type MyVariantType = {
    #type1;
    #type2;
};
public type MyRecord = {
    id: Nat;
    recordtype: MyVariantType;
};

In my public actor function I share an interface to pass in a vector of records, so:

public func pass_records (in_records : [MyRecord]) : async () {
    // do something
};

What object notation do I call the actor with in Javascript? I have tried the following.

[{"id":1, "recordtype": {"type1": true}}]
[{"id":1, "recordtype": {"type1": null}}]
[{"id":1, "recordtype": {"type1": {} }}]
[{"id":1, "recordtype": {"type1": true, "type2": false}}]

The first strategy seems to work when variants have some value to go with it.

The second one should work: [{"id":BigInt(1), "recordtype": {"type1": null}}]. Note that Nat corresponds to bigint in JS

#type1 corresponds to { type1: null }; and #type1: Nat corresponds to { type1: BigInt(42) }

4 Likes