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);
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}`); }); }
source share