Optional tuple elements

Similar to function signatures, we can also have optional tuple elements. This is achieved by using the ? symbol within a tuple definition, as follows:

let optionalTuple: [string, boolean?];
optionalTuple = ["test2", true];
console.log(`optionalTuple : ${optionalTuple}`);
optionalTuple = ["test"];
console.log(`optionalTuple : ${optionalTuple}`);

Here, we have a defined variable named optionalTuple that has a mandatory string property, and an optional boolean property. We then assign a value of ["test2", true] to it, and log it to the console. We are then assigning a value of ["test"] to the same tuple, and log the value to the console. As the second property of the optionalTuple is, in fact, optional, this code will compile cleanly, and produce the following results, as expected:

optionalTuple : test2,true
optionalTuple : test