Narrowing
Imagine we have a function called padLeft
.
function padLeft(padding: number | string, input: string): string { throw new Error("Not implemented yet!"); }Try
If padding
is a number
, it will treat that as the number of spaces we want to prepend to input
.
If padding
is a string
, it should just prepend padding
to input
.
Let's try to implement the logic for when padLeft
is passed a number
for padding
.
function padLeft(padding: number | string, input: string) { return new Array(padding + 1Operator '+' cannot be applied to types 'string | number' and '1'.).join(" ") + input; }TryOperator '+' cannot be applied to types 'string | number' and '1'.
Uh-oh, we're getting an error on padding + 1
.
TypeScript is warning us that adding a number
to a number | string
might not give us what we want, and it's right.
In other words, we haven't explicitly checked if padding
is a number
first, nor are we handling the case where it's a string
, so let's do exactly that.
function padLeft(padding: number | string, input: string) { if (typeof padding === "number") { return new Array(padding + 1).join(" ") + input; } return padding + input; }Try
If this mostly looks like uninteresting JavaScript code, that's sort of the point. Apart from the annotations we put in place, this TypeScript code looks like JavaScript. The idea is that TypeScript's type system aims to make it as easy as possible to write typical JavaScript code without bending over backwards to get type safety.
While it might not look like much, there's actually a lot going under the covers here.
Much like how TypeScript analyzes runtime values using static types, it overlays type analysis on JavaScript's runtime control flow constructs like if/else
, conditional ternaries, loops, truthiness checks, etc., which can all affect those types.
Within our if
check, TypeScript sees typeof padding === "number"
and understands that as a special form of code called a type guard.
TypeScript follows possible paths of execution that our programs can take to analyze the most specific possible type of a value at a given position.
It looks at these special checks (called type guards) and assignments, and the process of refining types to more specific types than declared is called narrowing.
In many editors we can observe these types as they change, and we'll even do so in our examples.
function padLeft(padding: number | string, input: string) { if (typeof padding === "number") { return new Array(padding + 1).join(" ") + input; ▲(parameter) padding: number } return padding + input; ▲(parameter) padding: string }Try
There are a couple of different constructs TypeScript understands for narrowing.
typeof
type guards
As we've seen, JavaScript supports a typeof
operator which can give very basic information about the type of values we have at runtime.
TypeScript expects this to return a certain set of strings:
"string"
"number"
"bigint"
"boolean"
"symbol"
"undefined"
"object"
"function"
Like we saw with padLeft
, this operator comes up pretty often in a number of JavaScript libraries, and TypeScript can understand it to narrow types in different branches.
In TypeScript, checking against the value returned by typeof
is a type guard.
Because TypeScript encodes how typeof
operates on different values, it knows about some of its quirks in JavaScript.
For example, notice that in the list above, typeof
doesn't return the string null
.
Check out the following example:
function printAll(strs: string | string[] | null) { if (typeof strs === "object") { for (const s of strsObject is possibly 'null'.) { console.log(s) } } else if (typeof strs === "string") { console.log(strs) } else { // do nothing } }TryObject is possibly 'null'.
In the printAll
function, we try to check if strs
is an object to see if it's an array type (now might be a good time to reinforce that arrays are object types in JavaScript).
But it turns out that in JavaScript, typeof null
is actually "object"
!
This is one of those unfortunate accidents of history.
Users with enough experience might not be surprised, but not everyone has run into this in JavaScript; luckily, TypeScript lets us know that strs
was only narrowed down to string[] | null
instead of just string[]
.
This might be a good segue into what we'll call "truthiness" checking.
Truthiness narrowing
Truthiness might not be a word you'll find in the dictionary, but it's very much something you'll hear about in JavaScript.
In JavaScript, we can use any expression in conditionals, &&
s, ||
s, if
statements, and Boolean negations (!
), and more.
As an example, if
statements don't expect their condition to always have the type boolean
.
function getUsersOnlineMessage(numUsersOnline: number) { if (numUsersOnline) { return `There are ${numUsersOnline} online now!`; } return "Nobody's here. :("; }Try
In JavaScript, constructs likeif
first "coerce" their conditions to boolean
s to make sense of them, and then choose their branches depending on whether the result is true
or false
.
Values like
0
NaN
""
(the empty string)0n
(thebigint
version of zero)null
undefined
all coerce to false
, and other values get coerced true
.
You can always coerce values to boolean
s by running them through the Boolean
function, or by using the shorter double-Boolean negation.
// both of these result in 'true' Boolean("hello"); !!"world";Try
It's fairly popular to leverage this behavior, especially for guarding against values like null
or undefined
.
As an example, let's try using it for our printAll
function.
function printAll(strs: string | string[] | null) { if (strs && typeof strs === "object") { for (const s of strs) { console.log(s) } } else if (typeof strs === "string") { console.log(strs) } }Try
You'll notice that we've gotten rid of the error above by checking if strs
is truthy.
This at least prevents us from dreaded errors when we run our code like:
TypeError: null is not iterable
Keep in mind though that truthiness checking on primitives can often be error prone.
As an example, consider a different attempt at writing printAll
function printAll(strs: string | string[] | null) { // !!!!!!!!!!!!!!!! // DON'T DO THIS! // KEEP READING // !!!!!!!!!!!!!!!! if (strs) { if (typeof strs === "object") { for (const s of strs) { console.log(s) } } else if (typeof strs === "string") { console.log(strs) } } }Try
We wrapped the entire body of the function in a truthy check, but this has a subtle downside: we may no longer be handling the empty string case correctly.
TypeScript doesn't hurt us here at all, but this is behavior worth noting if you're less familiar with JavaScript. TypeScript can often help you catch bugs early on, but if you choose to do nothing with a value, there's only so much that it can do without being overly prescriptive. If you want, you can make sure you handle situations like these with a linter.
One last word on narrowing by truthiness is that Boolean negations with !
filter out from negated branches.
function multiplyAll(values: number[] | undefined, factor: number): number[] | undefined { if (!values) { return values; } else { return values.map(x => x * factor); } }Try
Equality narrowing
TypeScript also uses switch
statements and equality checks like ===
, !==
, ==
, and !=
to narrow types.
For example:
function foo(x: string | number, y: string | boolean) { if (x === y) { // We can now call any 'string' method on 'x' or 'y'. x.toUpperCase(); ▲(parameter) x: string y.toLowerCase(); ▲(parameter) y: string } else { console.log(x); ▲(parameter) x: string | number console.log(y); ▲(parameter) y: string | boolean } }Try
When we checked that x
and y
are both equal in the above example, TypeScript knew their types also had to be equal.
Since string
is the only common type that both x
and y
could take on, TypeScript knows that x
and y
must be a string
in the first branch.
Checking against specific literal values (as opposed to variables) works also.
In our section about truthiness narrowing, we wrote a printAll
function which was error-prone because it accidentally didn't handle empty strings properly.
Instead we could have done a specific check to block out null
s, and TypeScript still correctly removes null
from the type of strs
.
function printAll(strs: string | string[] | null) { if (strs !== null) { if (typeof strs === "object") { for (const s of strs) { ▲(parameter) strs: string[] console.log(s); } } else if (typeof strs === "string") { console.log(strs); ▲(parameter) strs: string } } }Try
JavaScript's looser equality checks with ==
and !=
also get narrowed correctly.
If you're unfamiliar, checking whether something == null
actually not only checks whether it is specifically the value null
- it also checks whether it's potentially undefined
.
The same applies to == undefined
: it checks whether a value is either null
or undefined
.
interface Container { value: number | null | undefined } function multiplyValue(container: Container, factor: number) { // Remove both 'null' and 'undefined' from the type. if (container.value != null) { console.log(container.value); ▲(property) Container.value: number // Now we can safely multiply 'container.value'. container.value *= factor; } }Try
instanceof
narrowing
JavaScript has an operator for checking whether or not a value is an "instance" of another value.
More specifically, in JavaScript x instanceof Foo
checks whether the prototype chain of x
contains Foo.prototype
.
While we won't dive deep here, and you'll see more of this when we get into classes, they can still be useful for most values that can be constructed with new
.
As you might have guessed, instanceof
is also a type guard, and TypeScript narrows in branches guarded by instanceof
s.
function logValue(x: Date | string) { if (x instanceof Date) { console.log(x.toUTCString()); ▲(parameter) x: Date } else { console.log(x.toUpperCase()); ▲(parameter) x: string } }Try
Assignments
As we mentioned earlier, when we assign to any variable, TypeScript looks at the right side of the assignment and narrows the left side appropriately.
let x = Math.random() < 0.5 ? 10 : "hello world!"; ▲let x: string | number x = 1; console.log(x); ▲let x: number x = "goodbye!"; console.log(x); ▲let x: stringTry
Notice that each of these assignments is valid.
Even though the observed type of x
changed to number
after our first assignment, we were still able to assign a string
to x
.
This is because the declared type of x
- the type that x
started with - is string | number
, and assignability is always checked against the declared type.
If we'd assigned a boolean
to x
, we'd have seen an error since that wasn't part of the declared type.
let x = Math.random() < 0.5 ? 10 : "hello world!"; ▲let x: string | number x = 1; console.log(x); ▲let x: number xType 'true' is not assignable to type 'string | number'. = true; console.log(x); ▲let x: string | numberTryType 'true' is not assignable to type 'string | number'.
Control flow analysis
Up until this point, we've gone through some basic examples of how TypeScript narrows within specific branches.
But there's a bit more going on than just walking up from every variable and looking for type guards in if
s, while
s, conditionals, etc.
For example
function padLeft(padding: number | string, input: string) { if (typeof padding === "number") { return new Array(padding + 1).join(" ") + input; } return padding + input; }Try
padLeft
returns from within its first if
block.
TypeScript was able to analyze this code and see that the rest of the body (return padding + input;
) is unreachable in the case where padding
is a number
.
As a result, it was able to remove number
from the type of padding
(narrowing from string | number
to string
) for the rest of the function.
This analysis of code based on reachability is called control flow analysis, and TypeScript uses this flow analysis to narrow types as it encounters type guards and assignments. When a variable is analyzed, control flow can split off and re-merge over and over again, and that variable can be observed to have a different type at each point.
function foo() { let x: string | number | boolean; x = Math.random() < 0.5; console.log(x); ▲let x: boolean if (Math.random() < 0.5) { x = "hello"; console.log(x); ▲let x: string } else { x = 100; console.log(x); ▲let x: number } return x; ▲let x: string | number }Try
Discriminated unions
Most of the examples we've looked at so far have focused around narrowing single variables with simple types like string
, boolean
, and number
.
While this is common, most of the time in JavaScript we'll be dealing with slightly more complex structures.
For some motivation, let's imagine we're trying to encode shapes like circles and squares.
Circles keep track of their radii and squares keep track of their side lengths.
We'll use a field called kind
to tell which shape we're dealing with.
Here's a first attempt at defining Shape
.
interface Shape { kind: "circle" | "square"; radius?: number; sideLength?: number; }Try
Notice we're using a union of string literal types: "circle"
and "square"
to tell us whether we should treat the shape as a circle or square respectively.
By using "circle" | "square"
instead of string
, we can avoid misspelling issues.
function handleShape(shape: Shape) { // oops! if (shape.kind === "rect"This condition will always return 'false' since the types '"circle" | "square"' and '"rect"' have no overlap.) { // ... } }TryThis condition will always return 'false' since the types '"circle" | "square"' and '"rect"' have no overlap.
We can write a getArea
function that applies the right logic based on if it's dealing with a circle or square.
We'll first try dealing with circles.
function getArea(shape: Shape) { return Math.PI * shape.radiusObject is possibly 'undefined'. ** 2; }TryObject is possibly 'undefined'.
Under strictNullChecks
that gives us an error - which is appropriate since radius
might not be defined.
But what if we perform the appropriate checks on the kind
property?
function getArea(shape: Shape) { if (shape.kind === "circle") { return Math.PI * shape.radiusObject is possibly 'undefined'. ** 2; } }TryObject is possibly 'undefined'.
Hmm, TypeScript still doesn't know what to do here.
We've hit a point where we know more about our values than the type checker does.
We could try to use a non-null assertion (a !
after shape.radius
) to say that radius
is definitely present.
function getArea(shape: Shape) { if (shape.kind === "circle") { return Math.PI * shape.radius! ** 2; } }Try
But this doesn't feel ideal.
We had to shout a bit at the type-checker with those non-null assertions (!
) to convince it that shape.radius
was defined, but those assertions are error-prone if we start to move code around.
Additionally, outside of strictNullChecks
we're able to accidentally access any of those fields anyway (since optional properties are just assumed to always be present when reading them).
We can definitely do better.
The problem with this encoding of Shape
is that the type-checker doesn't have any way to know whether or not radius
or sideLength
are present based on the kind
property.
We need to communicate what we know to the type checker.
With that in mind, let's take another swing at defining Shape
.
interface Circle { kind: "circle"; radius: number; } interface Square { kind: "square"; sideLength: number; } type Shape = Circle | Square;Try
Here, we've properly separated Shape
out into two types with different values for the kind
property, but radius
and sideLength
are declared as required properties in their respective types.
Let's see what happens here when we try to access the radius
of a Shape
.
function getArea(shape: Shape) { return Math.PI * shape.radiusProperty 'radius' does not exist on type 'Shape'. Property 'radius' does not exist on type 'Square'. ** 2; }TryProperty 'radius' does not exist on type 'Shape'.Property 'radius' does not exist on type 'Square'.
Like with our first definition of Shape
, this is still an error.
When radius
was optional, we got an error (only in strictNullChecks
) because TypeScript couldn't tell whether the property was present.
Now that Shape
is a union, TypeScript is telling us that shape
might be a Square
, and Square
s don't have radius
defined on them!
Both interpretations are correct, but only does our new encoding of Shape
still cause an error outside of strictNullChecks
.
But what if we tried checking the kind
property again?
function getArea(shape: Shape) { if (shape.kind === "circle") { return Math.PI * shape.radius ** 2; ▲(parameter) shape: Circle } }Try
That got rid of the error! When every type in a union contains a common property with literal types, TypeScript considers that to be a discriminated union, and can narrow out the members of the union.
In this case, kind
was that common property (which is what's considered a discriminant property of Shape
).
Checking whether the kind
property was "circle"
got rid of every type in Shape
that didn't have a kind
property with the type "circle"
.
That narrowed shape
down to the type Circle
.
The same checking works with switch
statements as well.
Now we can try to write our complete getArea
without any pesky !
non-null assertions.
function getArea(shape: Shape) { switch (shape.kind) { case "circle": return Math.PI * shape.radius ** 2; ▲(parameter) shape: Circle case "square": return shape.sideLength ** 2; ▲(parameter) shape: Square } }Try
The important thing here was the encoding of Shape
.
Communicating the right information to TypeScript - that Circle
and Square
were really two separate types with specific kind
fields - was crucial.
Doing that let us write type-safe TypeScript code that looks no different than the JavaScript we would've written otherwise.
From there, the type system was able to do the "right" thing and figure out the types in each branch of our switch
statement.
As an aside, try playing around with the above example and remove some of the return keywords. You'll see that type-checking can help avoid bugs when accidentally falling through different clauses in a
switch
statement.
Discriminated unions are useful for more than just talking about circles and squares. They're good for representing any sort of messaging scheme in JavaScript, like when sending messages over the network (client/server communication), or encoding mutations in a state management framework.
The never
type
function getArea(shape: Shape) { switch (shape.kind) { case "circle": return Math.PI * shape.radius ** 2; case "square": return shape.sideLength ** 2; } }Try