How to include output parameters in a function using Typescript?

Is it possible to include output parameters in a function using TypeScript? Something like Func1(string val1, int out k1, int out k2) in C #.

+17
source share
7 answers

Not at present.

You can return an object that may contain more than one property.

 return { k1: 5, k2: 99 }; 
+14
source

Typically, you simply return an object with several properties, one of which contains your function. Something like that:

 var foo = function (val1 : string){ // do something return { k1: 22, k2: 33 }; } 

You can also implement an interface, so you know what to expect as a return object.

 interface IFoo { (val1: string): INumbers; } interface INumbers { k1 : number; k2 : number; } var foo : IFoo = (val1 : string){ // do something return { k1: 22, k2: 33 }; } 
+1
source

Typescript passes all parameters using a "call by value". But if the parameter is a link, it behaves in the same way as a "call by reference" most of the time. You can write wrapper classes for primitive types. Here is the code:

 var func=function(param:Str){ param.str="modified"; } class Str{ str:string="unmodified"; } var test:Str=new Str(); alert(test.str); //alerts "unmodified" func(test); alert(test.str); //alerts "modified" 

You need to be careful:

 var func=function(param:Str){ param=new Str("modified"); } class Str{ str:string; constructor(param:string){ this.str=param; } } var test:Str=new Str("unmodified"); alert(test.str); //alerts "unmodified" func(test); alert(test.str); //alerts "unmodified" 

The function parameter is passed "call by value". Therefore, inside the function body, you are working with a copy of the link. This link points to the same object as the link that you passed as a parameter, so that you can access your members and change them. But if you assign a new object to a link, all new changes are applied to this new object. Therefore, the above code prints unmodified data twice. I think C # works like this too.

+1
source

If you really want to have an output parameter, even if you can return an object or an array (as a temporary tuple object), look at foo and the call site of foo ...

 function p(s) { document.body.appendChild(document.createTextNode(s)); document.body.appendChild(document.createElement('BR')); } function foo(output: any): void { output.uno = 1; output.dos = 2; } var o: any = {}; function foo(o); p(o.uno + " " + o.dos); 
+1
source

If you want to use C # type syntax you can use:

 function func(val1, k1, k2) { k1.v = 7; k2.v = 9; return ""; } 

and call him

 func("", {}, {}); 
0
source

Sometimes the parameter is undefined and you need to create it inside the method. In this case, you can use "lambda functions" or "arrow functions" and simulate the output parameter as follows:

Example:

 class classA { propertyA : number; constructor(value: number){ propertyA = number; } } class classB { // ... exampleMethod(){ let classAInstance: classA; this.sumValueMethod((p) => classAInstance = p, 10); if(classAInstance != undefined) alert("Yeah"); } sumValueMethod(paramA:(p: classA) => any, paramB: number){ let variableA: classA = new classA(0); variableA.propertyA += paramB; paramA(variableA); } } 
0
source

Here is another way. Define a callback function that will include your parameters:

 function Func1(val1: string, out: (k1: number, k2: number) => void): void { out(1, 2); } 

An example of how to use it:

 function anotherFunction(): void { let k1: number; let k2: number; Func1("something", (v1, v2) => { k1 = v1; k2 = v2; }); console.log(k1); // output: 1 console.log(k2); // output: 2 } 

I find it mostly useful for things like this:

 const keys: string[] = []; const values: number[] = []; function tryGet(key: string, out: (value: number) => void): void { const index = keys.indexOf(key); if (index >= 0) { out(values[index]); } } function test(): void { const key = "myValue"; tryGet(key, (value) => { console.log(`Key '${key}' exist with value ${value}`); }); } 
0
source

Source: https://habr.com/ru/post/1243769/


All Articles