Here is an example of an overloaded Typescript function:
function clearField(o : Record<"field", string>, nullify : false) : void
function clearField(o : Record<"field", string | null>, nullify : true) : void
function clearField(o : any, nullify : any) : void
{
if (nullify)
{
o.field = null;
}
else
{
o.field = "";
}
}
Now, let's take a look at this object:
let o : Record<"field", string> = {"field" : "toBeCleared"};
Unfortunately, the following code will not produce any errors:
clearField(o, true); // 'o.field' is changed to null even though it was declared as type string.
To fix this issue and receive a Typescript error in strict mode, what changes can be made to the function declarations/definitions? The goal is to prevent the widening of Record<"field", string> to Record<"field", string | null> when passed to "clearField," maintaining contravariance for the values in that context.