JavaScript has two ways to say nothing is here, and every TypeScript codebase quietly picks a policy about them, usually without anybody writing it down. I had never written mine down either. So instead of deciding what it should be, I counted what it is.
The subject is Nakodo: 520 non-test TypeScript files, 68,546 lines, a Next 16 app over Postgres.
How it was measured
Word-boundary regular expressions over the source, counting occurrences of each keyword, then the same over the type positions and the comparison forms separately. Tests are excluded throughout, because a test file is full of fixtures that would skew every ratio.
Two traps. The first is that null appears inside strings, SQL fragments and comments, so a bare grep -c null overcounts; I required a non-identifier character on both sides, which still counts the word in a comment and I have accepted that as noise rather than pretend to parse. The second is that undefined appears in error messages and in type-level text, which is the same problem in the other direction. The headline counts are therefore approximate within a percent or two, and the comparison counts, which are specific syntax, are exact.
The counts
null | undefined | |
|---|---|---|
| Occurrences | 2,119 | 275 |
| Files containing it | 228 | 113 |
| In a union type (`\ | X`) | 787 |
return of it | 167 | 7 |
Assigned (= X) | 310 | 38 |
Seven and a half to one, in a codebase where nobody ever discussed it. Plus 682 optional markers (?:) on properties and parameters, which is the third way to say nothing is here and the one that produces undefined without the word appearing anywhere.
At the file level the split is cleaner than the totals suggest:
- 131 files mention
nulland neverundefined - 16 files mention
undefinedand nevernull - 276 files mention neither
Over half the codebase never says either word. That is not restraint, it is the optional chain and the nullish coalescing operator: 674 uses of ?. and 726 of ??, each of which handles both empties without naming one.
Where the ratio comes from
The database. This app has 25 tables and 318 column declarations, 147 of which are nullable, and the ORM types a nullable column as T | null. That type then travels: out of the query, through the function that shapes the row, into the component that renders it. 787 union types saying | null is not a style, it is the schema arriving in the type system and never being converted on the way.
You can watch it happen on a public page. Here is the calculator widget, which is the file with the fourth highest count of the word null in the repository:
value={rate === null ? "n/a" : pct(rate)} Open the engagement rate calculator and the three result tiles say "n/a". Type a view count into the first row and they become numbers. null there is not a database value, it is the same convention applied one layer out: the function that computes a rate returns number | null, because a rate with no views is not zero, it does not exist. The code that renders it has one branch and no clever fallback, which is the whole benefit of having picked one empty.
Three more places the choice shows up in the open:
- 59 uses of the ORM's
isNullandisNotNullhelpers in query predicates, because SQL'sIS NULLhas no second form to choose between. - 30 schema validators marked
.nullable()against 9 marked.optional(), and zero.nullish(). Every boundary where data comes in from outside has been told which empty to expect, and in three cases out of four the answer is null. - 103 expressions ending
?? null, which is the conversion point: something that might be absent becomes something that is explicitly empty before it is stored or returned.
That last pattern is the one I would defend hardest. ?? null looks like a no-op and is the opposite: it is the line where an optional thing becomes a definite nothing, so that everything downstream has one case to handle instead of two.
232 strict comparisons and 47 loose ones
| Form | Count |
|---|---|
=== null | 131 |
!== null | 101 |
=== undefined | 14 |
!== undefined | 24 |
== null | 28 |
!= null | 19 |
The 47 loose comparisons are the interesting ones, because == null is the single most-linted idiom in JavaScript and it is in here on purpose. In JavaScript x == null is true for exactly two values, null and undefined, and nothing else. That makes it the only one-expression way to ask "is this absent for either reason", and every one of the 47 is in a place where both are possible:
{r.medianViews == null ? "n/a" : formatCount(r.medianViews)} medianViews is number | null from the database. The row it sits in is sometimes built by a function with an optional field. One check covers both, and the alternative is === null || === undefined on every render, or a cast that asserts something the types do not know.
The reason that is allowed to stay is that nothing forbids it: our ESLint configuration is the framework's two presets and a list of ignores, with no rule of our own, so eqeqeq is not on. I went looking for the rule I thought I had and did not have. Given 47 deliberate uses and zero accidental ones, the rule I actually want is eqeqeq with "allow-null", which permits exactly this form and rejects every other loose comparison. That is a one-line change and it is now on the list.
Where undefined genuinely wins
undefined is not a style failure here, it is TypeScript's own word for "absent", and it turns up in exactly the places where absence is the concept:
- 682 optional markers. An optional parameter is
undefinedwhen it is not passed, and there is no alternative spelling. - The spread idiom for building options objects, where a key whose value is
undefinedis meant to mean "not specified". The strict flag that would distinguish "property absent" from "property present and undefined" produces 110 errors in this codebase, and I have decided not to turn it on, which is a decision about this exact distinction. - The top
undefinedfile in the repository is the campaigns actions file with 18, and they are all optional arguments to functions called from several screens with different amounts of information.
So the policy, written down for the first time: null is a value we store and return, undefined is what an argument is when it was not given, and ?? null is the border between them. 2,119 to 275 is that policy, enforced by nothing except that the database only speaks one of the two languages.
What I take from this
- The schema is the style guide. 147 nullable columns produced 787 union types and 232 strict comparisons. If you want a codebase with fewer nulls in it, the lever is the column definition, not a lint rule.
-
?? nullis the pattern I did not know I had. 103 of them, all doing the same job: converting TypeScript's absence into the database's emptiness at the boundary, so that no function has to handle both. Worth naming, because once you can name it you can notice the places that skipped it. - Turn on
eqeqeqwith"allow-null". 47 deliberate loose comparisons and no rule protecting them means the next one could be== 0or== ""and nothing would say anything. The idiom is good; it should be the only loose comparison permitted. - 276 files mention neither empty. That is the real finding about modern TypeScript:
?.and??have made the choice invisible in most code, which is why the choice goes unexamined for years. It is still a choice, and it is still better made once than five hundred times.
If you want to run this on your own repo, count the union types rather than the keyword occurrences. | null against | undefined against ?: is three numbers, it takes one command, and the ratio between them tells you which layer of your stack is writing your types.