-2

I need in typescript (or, at least, javascript) a function which I can call/use like an array: myFunc[someInput]

I know I cand do something like:

interface IInputs {
    "someInput1": someType,
    "someInput2": someType,
}
interface someType {
    // ...
}
var myFunc: IInputs;

So I may write:

myFunc["someInput1"]

The first problem is that I have to hard code each possible input. Input is a string but can be any string. Another is that my function should call another one (a 'normal' one) simply like this:

myFunc[input: string] : any {
    //...
    return anotherFunc(input);
}

I have simplified my problem to show only the relevant facts.

Gigi
  • 158
  • 11
  • 2
    what do you mean with *array like* and what has a function to do with it? – Nina Scholz Apr 10 '19 at 15:02
  • What is your question again? – Isaac Vidrine Apr 10 '19 at 15:06
  • What you are proposing does not exist in JavaScript (or TypeScript). This is because you [cannot overload the `[]` index operator](https://stackoverflow.com/questions/1711357/how-would-you-overload-the-operator-in-javascript). – Tim Klein Apr 10 '19 at 15:06
  • It is just syntax: I need a function. But I don't want to use it like myFunc('bla'); but myFunc['bla']; instead. – Gigi Apr 10 '19 at 15:07
  • So you want to access an object value by its key? If not, whats the point? – Isaac Vidrine Apr 10 '19 at 15:12
  • 2
    *Why* do you want to change the syntax from a method call to an array access? What benefit does this give you? –  Apr 10 '19 at 15:18
  • Are you to tell us that you wish to change the language's syntax for no other reason than to make it look like another language? That you want to mess things up not just for you but also for your team and any future person that will have to deal with your code? –  Apr 10 '19 at 16:44
  • @davmich The reason is that I want to use this together with intellisense in Monaco Editor. There I can write something like someObj.myFunc['hello'].someProp and while inside quotes, ctrl+space will open a drop down with possible myFunc arguments. The list for autocompletion I build at runtime depending on the available options in a context or another. But in order for this to really work, the real code has to look the same hence the starnge look for that function. If I put a normal function syntax, intellisense won't work the way I want. – Gigi Apr 10 '19 at 17:31

1 Answers1

2

Looks like you're looking for Proxies:

function someOtherFunc(prop: string) {
    return "you said " + prop;
}

interface Indexable {
    [key: string]: any;
}


let myFunc: Indexable = new Proxy({}, {
    get(_, prop: string) {
        return someOtherFunc(prop);
    }
});


console.log(myFunc['hello'])
console.log(myFunc['hey'])

JS version:

    function someOtherFunc(prop) {
        return "you said " + prop;
    }
    
    let myFunc = new Proxy({}, {
        get(_, prop) {
            return someOtherFunc(prop);
        }
    });
    
    
    console.log(myFunc['hello'])
    console.log(myFunc['hey'])
georg
  • 211,518
  • 52
  • 313
  • 390
  • 1
    While this is a correct answer, it would be preferable that no one seriously answers @Gigi's question when said person clearly doesn't know what he's doing. Informing him of functionalities that he both doesn't need to know and to use is a recipe for disaster. –  Apr 10 '19 at 15:44
  • @davmich The reason is that I want to use this together with intellisense in Monaco Editor. There I can write something like someObj.myFunc['hello'].someProp and while inside quotes, ctrl+space will open a drop down with possible myFunc arguments. The list for autocompletion I build at runtime depending on the available options in a context or another. But in order for this to really work, the real code has to look the same hence the starnge look for that function. If I put a normal function syntax, intellisense won't work the way I want. – Gigi Apr 10 '19 at 17:32