typescript is there a way to pass parameter to method by reference?

With JavaScript, and TypeScript, you can pass an object by reference — but not a value by reference. Therefore box your values into an object.

So instead of:

 function foo(value1: number, value2: number) {
     value1++;
     value2++;
 }

Do:

function foo(model: {property1: number; property2: number}) {
     model.property1++;
     model.property2++;

     // Not needed but
     // considered good practice.
     return model;
}

const bar = { property1: 0, property2: 1 };
foo(bar);

console.log(bar.property1) // 1
console.log(bar.property2) // 2

See full demo on TS Playground

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)