flip
Flip the value of a boolean without having to reference it twice.
Last updated
Was this helpful?
Flip the value of a boolean without having to reference it twice.
import { flip } from "tsafe/flip";
const obj = {
is_: false,
};
flip(obj, "is_");
//obj.is_ is now set to trueWhen you have an object that contains another object that contains a boolean, and you wish to flip the value of that boolean for example, you would usually do as follows.
const obj = {
innerObj: {
is_: false,
x: 44,
y: 33,
},
};
obj.innerObj.is_ = !obj.innerObj.is_;With the flip function, the task will be a bit less tedious.
The first argument is the object that contains the boolean value, and the second, is the key of the boolean property. Typescript will infer the key(s) of type boolean as illustrated below.

Last updated
Was this helpful?
Was this helpful?
flip(obj.innerObj, "is_");