My goal is to set p1 to null after calling setNull().
You can't, because it isn't "passed by reference." It's an object reference passed by value. "Pass by reference" is a term of art meaning a reference to the variable is passed into the function. Neither TypeScript nor JavaScript has pass-by-reference, at all.
In this specific case, it makes sense just to assign null
to p1
. But if you had a more general case, perhaps if setNull
doesn't always set it null
, or does something else (the classic case is "closing" p1
in some way), you could use the return value of setNull
to set p1
, e.g.:
p1 = someFunction(p1);
...where someFunction
returns a value to use to update p1
(perhaps null
).
Alternately, make p1
a property of a mutable object, pass that object in, and have the function set the p1
property to null
where appropriate.
But if you're unconditionally assigning null
without the function having any other reason for being, just...do that, assign null
to p1
without a function.