What's the "("0, ") idiom in JS?

What is the Reason for the (0, Pattern in Transpiled JavaScript?

When looking at transpiled JavaScript from TypeScript, I often see the pattern (0, someFunction)(...). For example, this TypeScript code:

await formatFiles(tree);

compiles to this JavaScript:

await (0, _devkit.formatFiles)(tree);

I’m wondering what is the reason for the (0, pattern? As far as I understand the comma operator, it evaluates each expression, throws away the value of all but the last, and returns that last value. However, the expression 0 doesn’t have side effects; it doesn’t do anything. So, (0, _devkit.formatFiles) is equivalent to _devkit.formatFiles.

Am I missing something? Does the (0, pattern actually do something?

The (0, pattern in transpiled JavaScript serves as a way to invoke a function with a specific context or scope. It is commonly used to ensure that the function is called with a particular this value.

In the case you provided, (0, _devkit.formatFiles) is used to invoke the _devkit.formatFiles function with the this value set to 0. This is useful when the function being called relies on the this context, such as when it is a method of an object.

The use of (0, is a workaround to avoid losing the reference to the intended this value when calling the function. It ensures that the function is invoked with the correct context while still discarding the 0 value.

So, the (0, pattern does have a purpose in this context, allowing the function to be called with a specific this value.