JavaScript API design for generating a ASN.1 data object.

They differ. UTCTime has only 2 digits to represent the year, while GeneralizedTime has 4 digits, therefore UTCTime is not a subset of GeneralizedTime. I think this is an outdated example to use Choice because it was provided for y2k compatibility reason: the old schema required UTCTime, then at the turn of the century the schema gets updated so the old data signed with UTCTime is still valid and doesn't have to be changed and signed again. New developers should always choose to use GeneralizedTime. I recommend using a different example:

<namedType name="Subscription">
   …
   <namedType name="expiry">
     <type>
       <choice>
   		 <element name="blockHeight" type="asnx:Integer"/>
   		 <element name="generalizedTime" type="asnx:GeneralizedTime"/>
       </choice>
     </type>
   </namedType>
   …
</namedType>

This example is about a subscription that allows a vendor (e.g. a magazine) to take money from your account at a certain interval (once a week). It has an expiry that can be either the block height or a specific time. Contrast to the previous example, in this example the higher-level data structure is mentioned, so you have an object containing the Choice.

With @hboon's ES5 style taken in as well, the code is:

let tx = new Subscription()
tx.expiry = new UTCTime("20200704")

Or

let tx = new CrossChainTransaction()
tx.expiry = new BlockHeight(1212000)

And the choice is thus made in the set expiry function (generated from the ASN.X piece provided).


Is there any advantage in designing the API pretending that Choice is an object (which has as many attributes as the choices and only one is not null)? With the design I proposed, a developer may get:

let expiry = tx.expiry

Without knowing what type this expiry is. It may be a GeneralizedTime, or it may be an Integer, per choice made before. If the developer didn't check and mistaken an integer (blockHeight) to a timestamp. Would pretending Choice an object (instead of making the choice when setting the value) help in solving this issue?