0

So I'm creating some functions dynamically based on some input from the users. One input is the name of each function. I would like to be able to create (and expose) some functions with the same name but different parameters.

const input = [
      {name: 'mul', paramTypes: { par1: 'number', par2: 'number'},
      {name: 'add', paramTypes: { par1: 'number', par2: 'number'},
      {name: 'add', paramTypes: { par1: 'string', par2: 'string'}
];

let functionsToExpose = [];

for (fun in input){

     functionsToExpose[fun.name] = (...arguments)=>{...}

}
return functionsToExpose;

I would like to create two add functions, one for numbers and one for strings (concatenation). Currently, only one is created. Is there a way I could create and expose both?

3
  • What specifically are you having trouble with? Commented Oct 16, 2022 at 15:21
  • Can you be more specific about "input from users"? Commented Oct 16, 2022 at 15:22
  • Please provide a minimal reproducible example that demonstrates your issue when pasted, as-is, into a standalone IDE. Right now it's pseudocode: the fun loop var gets keys "0", "1", and "2"; functionsToExpose is apparently an array; the conceptual issue for how someone could ask for a function implementation merely by name and parameter types is overlooked completely; etc. Without a toy example that at least begins to work I don't know how to advise, or at least I can't test it against any use case. If you edit to include a minimal reproducible example and want me to take another look, pls mention @jcalz in your reply. Commented Oct 16, 2022 at 15:38

1 Answer 1

2

So I'm creating some functions dynamically based on some input from the users.

Then TypeScript won't be able to help with this part, because TypeScript works at compile-time, and you won't have the data from the users except at runtime.

I would like to create two add functions, one for numbers and one for strings (concatenation).

The only way you can do that in JavaScript is if you store the functions in different places. It's impossible, in JavaScript, to have more than one function associated with the same identifier in the same scope. (The functions can have the same name as far as the name property on them goes, but they have to be referenced by distinct things.) TypeScript could let it seem like you had function overloads, but there would be only one actual function (the implementation), and again TypeScript type information is a compile-time construct, not a runtime construct.

There are various other things you can do:

  • Include the parameter types in the name
  • Organize the functions into separate objects (numbers.add, strings.add)
  • Create an array of functions
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.