Founder Notes
A Comment That Compiles
I wrote a doc comment last week that I thought was documentation. It was a type declaration, and I had been approximate in it.
The module was small and dull: a rule that decides which records have gone stale and ought to be put in front of a person. Plain JavaScript, a couple of exported functions, and above them the kind of comment I have written ten thousand times — here is what this takes, here is what it gives back.
I described the rows it returns as having three fields: where the record came from, the date in question, and a status. I typed the return as a list of objects and moved on. It was a fair summary of what the function did. I was describing my own code to whoever read it next.
There was no signature for it to be a summary of
Our typechecker derives the types of those plain-JavaScript modules from exactly those comments. Not as a hint, not as a fallback when it cannot work something out — as the source. The comment is where the type comes from, and there is nowhere else.
So the comment was not a description of the module's interface. It was the module's interface, binding at every call site, and it had been written in the register you use for prose.
"Too narrow" is the wrong description of what went wrong
My instinct was to say the comment left something out. It did — the record's own identifier, the one field the whole thing is useless without. But leaving something out is what an incomplete description does, and that is not what happened here.
A declaration does not get to be partial. Mine said: these rows have exactly these three fields, and this function hands back objects about which nothing further is known. That is a positive statement, and it was false. Everything downstream believed it, correctly. Fixtures that carried a record identifier became objects with a field that was not supposed to exist. Code that walked the results and reached for the identifier had to be talked out of its own correctness with a cast. None of those call sites was wrong. They were all faithfully obeying a contract I thought I was narrating.
The same mistake, in the opposite direction, in the same file
Here is the part I keep turning over. I had also written suppression directives above the imports of that module — the little comments that tell the typechecker to look away from the next line — because I assumed it could not resolve a plain-JavaScript module in the first place.
It resolved them without complaint. Which made the suppressions errors in their own right: a suppression for a problem that does not exist is itself a problem, and the tool says so.
So one wrong belief produced two opposite failures within a few lines of each other. I had been loose in a comment because I thought nothing read it, and I had silenced errors that were never raised because I thought nothing could see the file. Both are the same mistake about what kind of text I was writing.
The fix was to say the true thing, not to quiet the objections
The repair was dull and worth stating anyway: name the row shape properly, including the identifier, and write the return types so the identifier survives the trip out.
The identifier belongs in that shape even though the module never reads it. The module does not need it; the person opening the queue does, and a list of problems nobody can open is not a work queue. A contract exists for the benefit of the side that consumes it, which is the thing I lose sight of whenever I write one from the inside out.
A couple of call sites still carry a deliberate cast with a note saying why — they exist precisely to hand the module values the declaration forbids and prove it refuses them. A declaration in a comment is advice the typechecker takes; it is not a guard at runtime, and those two facts live happily together once you stop confusing them.
The general form
Descriptions and declarations are the same characters in the same place. Nothing about the text tells you which one you are writing. The only thing that decides is whether something reads it — and whether something reads it is a property of your toolchain, not of your prose.
This is not a quirk of one language's comment convention. Docstrings that generate schemas, annotations that configure a framework, the description fields in infrastructure definitions, the comments a build step parses: all of them look exactly like commentary and none of them are. Every one of them is a place where being roughly right is indistinguishable from being precisely wrong.
So the question I had been asking — is this comment accurate enough? — was the wrong one. The question is whether anything in the system reads it. A description can be approximate. A declaration cannot, and it will not tell you which one you have written.