I'm looking at some code that's been transpiled from TypeScript to JavaScript, and I keep seeing the pattern (0, someFunction)(...)
. For example, this TypeScript: (source)
await formatFiles(tree);
compiles to this JavaScript: (from the node_modules folder of the project I'm importing it into)
await (0, _devkit.formatFiles)(tree);
(I don't know exactly how the TS was transpiled to JS; I just saw the weird JS in my node_modules directory and looked up the source. But I can tell that the added _devkit.
is from how the TypeScript import
s were compiled to CommonJS require
s.)
My question is: what's up with that (0,
pattern?
On the one hand, 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. Which matters if the earlier expressions have side effects. But the expression 0
doesn't have side effects; it doesn't do anything. (0, _devkit.formatFiles)
, as far as I understand it, is exactly equivalent to _devkit.formatFiles
.
On the other hand, I assume the folks who wrote the TypeScript compiler are smart people, and that they added the (0,
for a reason. I just can't fathom what it is.
Am I missing something? What is the reason for the zero? Does it actually do something?