I have an invoice that can be in the states New/Invoiced. I have a doInvoicing method that takes a New Invoice and return an Invoiced invoice. But I can't change the state of the invoice in my method, due to it being bound to the New state.
Currently I can only think of casting the input invoice to Invoiced. However, this leaves a hole in the type checking, as it does not verify that I set the invoice state to Invoiced. I am thinking the operation required for this must combine the steps of casting/setting the value (if possible).
doInvoicing(invoice: Invoice & { state: invoiceState.New }):
Invoice & { state: invoiceState.Invoiced } {
var invoiced = invoice as Invoice & { state: invoiceState.Invoiced };
invoiced.state = invoiceState.Invoiced; // This MUST happen, but unverified
return invoiced;
}
enum invoiceState {
New, Invoiced
}