Is there a way in TypeScript to create a custom type with methods that can access the variable's value directly, similar to how Array methods work without passing the value as a parameter?
For example:
const arrayVar: Array = [1,2,3];
array.find(el => el === 1);
In this case, the find method has access to the values of the array arrayVar
without needing it to be passed as a parameter. I would like to achieve something similar with a custom type, for instance:
const myVar: MyCustomType = 5;
myVar.add(2); // In this case, the return would be 7.
I understand that this could be achieved using classes and functions but then I would have to pass the value of "MyVar" as a parameter (function add(value1, value2)
, for example). I am looking for a way to directly access the variable's value just like Array does in its methods.