Currently, the Amount constructor and convertTo function both take rounding options.
As a result, these operations may or may not convert the input to a different data type (a string). This seems challenging for TypeScript and generally for code readability:
function convert(options) {
let a = new Amount({ value: 1.23, unit: "meter" });
let b = a.convertTo(options);
return b;
}
In that code, is b a Number Amount or is it a String Amount? It can't be known without inspecting options.
By having an explicit roundTo function and removing rounding options from the constructor and convertTo, the flow of data types is much more clear.
The main downside of such a model is that you end up with an extra intermediate object: a.convertTo(convertOptions).roundTo(roundOptions) has a short-lived intermediate.
Currently, the Amount constructor and
convertTofunction both take rounding options.As a result, these operations may or may not convert the input to a different data type (a string). This seems challenging for TypeScript and generally for code readability:
In that code, is
ba Number Amount or is it a String Amount? It can't be known without inspectingoptions.By having an explicit
roundTofunction and removing rounding options from the constructor andconvertTo, the flow of data types is much more clear.The main downside of such a model is that you end up with an extra intermediate object:
a.convertTo(convertOptions).roundTo(roundOptions)has a short-lived intermediate.