Check argument names in function calls #46455
Labels
Out of Scope
This idea sits outside of the TypeScript language design constraints
Suggestion
An idea for TypeScript
Uh oh!
There was an error while loading. Please reload this page.
Suggestion
π Search Terms
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
This suggestion is very similar to #467, but it has another purpose and motivation.
I'd like to have an ability to specify argument names in function calls. TypeScript will emit an error if an argument name in a function call expression doesn't match the argument name in the function declaration.
TypeScript compiler will just remove the argument names from the function invocation expressions. The argument names in call expressions don't affect the output code at all.
This idea can be spread further on arrays with named elements, but this is off-topic here.
π Motivating Example
The motivation is a need to hide my source code. Let's say I use TypeScript with Terser, I need to make a function with many arguments and hide the argument names from the output code.
The following source code will give the result I want:
But it's prone to human mistake because all the arguments have the same type so there will be no TypeScript error if a pair of arguments in a function call is swapped.
A common solution for such mistakes is passing arguments in an object:
That it, the secret argument names have got to the output code. Also, the output code is much bigger.
The syntax I suggest will solve both problems (will conceal the argument names and help preventing human mistakes):
π» Use Cases
Besides concealing the source code, the new syntax will help producing smaller JS scripts. Developers will achieve it by replacing argument objects with regular arguments and function call checks.
At the moment I can solve my issue by taking a risk of mistakes by using regular arguments as shown in the first code snippet in the "Motivating Example" section.
Another solution is using an argument object with a const enum which is bulky:
The text was updated successfully, but these errors were encountered: