Typescript Handbook
Typescript Handbook
The most common kinds of errors that programmers write can be described as type errors: a
certain kind of value was used where a different kind of value was expected. This could be due to
simple typos, a failure to understand the API surface of a library, incorrect assumptions about
runtime behavior, or other errors. The goal of TypeScript is to be a static typechecker for JavaScript
programs - in other words, a tool that runs before your code runs (static) and ensures that the types
of the program are correct (typechecked).
If you are coming to TypeScript without a JavaScript background, with the intention of TypeScript
being your first language, we recommend you first start reading the documentation on JavaScript
at the Mozilla Web Docs. If you have experience in other languages, you should be able to pick up
JavaScript syntax quite quickly by reading the handbook.
The Handbook
You should expect each chapter or page to provide you with a strong understanding of the given
concepts. The TypeScript Handbook is not a complete language specification, but it is intended to
be a comprehensive guide to all of the language's features and behaviors.
In the interests of clarity and brevity, the main content of the Handbook will not explore every
edge case or minutiae of the features being covered. You can find more details on particular
concepts in the reference articles.
Reference Files
The reference section below the handbook in the navigation is built to provide a richer
understanding of how a particular part of TypeScript works. You can read it top-to-bottom, but
each section aims to provide a deeper explanation of a single concept - meaning there is no aim
for continuity.
Non-Goals
The Handbook is also intended to be a concise document that can be comfortably read in a few
hours. Certain topics won't be covered in order to keep things short.
Specifically, the Handbook does not fully introduce core JavaScript basics like functions, classes, and
closures. Where appropriate, we'll include links to background reading that you can use to read up
on those concepts.
The Handbook also isn't intended to be a replacement for a language specification. In some cases,
edge cases or formal descriptions of behavior will be skipped in favor of high-level, easier-to-
understand explanations. Instead, there are separate reference pages that more precisely and
formally describe many aspects of TypeScript's behavior. The reference pages are not intended for
readers unfamiliar with TypeScript, so they may use advanced terminology or reference topics you
haven't read about yet.
Finally, the Handbook won't cover how TypeScript interacts with other tools, except where
necessary. Topics like how to configure TypeScript with webpack, rollup, parcel, react, babel, closure,
lerna, rush, bazel, preact, vue, angular, svelte, jquery, yarn, or npm are out of scope - you can find
these resources elsewhere on the web.
Get Started
Before getting started with Basic Types, we recommend reading one of the following introductory
pages. These introductions are intended to highlight key similarities and differences between
TypeScript and your favored programming language, and clear up common misconceptions
specific to those languages.
TypeScript for New Programmers
Each and every value in JavaScript has a set of behaviors you can observe from running different
operations. That sounds abstract, but as a quick example, consider some operations we might run
on a variable named message .
// Calling 'message'
message();
If we break this down, the first runnable line of code accesses a property called toLowerCase
and then calls it. The second one tries to call message directly.
But assuming we don't know the value of message - and that's pretty common - we can't reliably
say what results we'll get from trying to run any of this code. The behavior of each operation
depends entirely on what value we had in the first place.
Is message callable?
The answers to these questions are usually things we keep in our heads when we write JavaScript,
and we have to hope we got all the details right.
As you can probably guess, if we try to run message.toLowerCase() , we'll get the same string
only in lower-case.
What about that second line of code? If you're familiar with JavaScript, you'll know this fails with an
exception:
When we run our code, the way that our JavaScript runtime chooses what to do is by figuring out
the type of the value - what sorts of behaviors and capabilities it has. That's part of what that
TypeError is alluding to - it's saying that the string "Hello World" cannot be called as a
function.
For some values, such as the primitives string and number , we can identify their type at
runtime using the typeof operator. But for other things like functions, there's no corresponding
runtime mechanism to identify their types. For example, consider this function:
function fn(x) {
return x.flip();
}
We can observe by reading the code that this function will only work if given an object with a
callable flip property, but JavaScript doesn't surface this information in a way that we can check
while the code is running. The only way in pure JavaScript to tell what fn does with a particular
value is to call it and see what happens. This kind of behavior makes it hard to predict what code
will do before it runs, which means it's harder to know what your code is going to do while you're
writing it.
Seen in this way, a type is the concept of describing which values can be passed to fn and which
will crash. JavaScript only truly provides dynamic typing - running the code to see what happens.
The alternative is to use a static type system to make predictions about what code is expected
before it runs.
Static type-checking
Think back to that TypeError we got earlier from trying to call a string as a function. Most
people don't like to get any sorts of errors when running their code - those are considered bugs!
And when we write new code, we try our best to avoid introducing new bugs.
If we add just a bit of code, save our file, re-run the code, and immediately see the error, we might
be able to isolate the problem quickly; but that's not always the case. We might not have tested the
feature thoroughly enough, so we might never actually run into a potential error that would be
thrown! Or if we were lucky enough to witness the error, we might have ended up doing large
refactorings and adding a lot of different code that we're forced to dig through.
Ideally, we could have a tool that helps us find these bugs before our code runs. That's what a static
type-checker like TypeScript does. Static types systems describe the shapes and behaviors of what
our values will be when we run our programs. A type-checker like TypeScript uses that information
and tells us when things might be going off the rails.
message();
This
This expression
expression is
is not
not callable.
callable.
Type
Type 'String'
'String' has
has no
no call
call signatures.
signatures.
Running that last sample with TypeScript will give us an error message before we run the code in
the first place.
Non-exception Failures
So far we've been discussing certain things like runtime errors - cases where the JavaScript runtime
tells us that it thinks something is nonsensical. Those cases come up because the ECMAScript
specification has explicit instructions on how the language should behave when it runs into
something unexpected.
For example, the specification says that trying to call something that isn't callable should throw an
error. Maybe that sounds like "obvious behavior", but you could imagine that accessing a property
that doesn't exist on an object should throw an error too. Instead, JavaScript gives us different
behavior and returns the value undefined :
const user = {
name: "Daniel",
age: 26,
};
const user = {
name: "Daniel",
age: 26,
};
user.location;
Property
Property 'location'
'location' does
does not
not exist
exist on
on type
type '{
'{ name:
name: string;
string; age:
age: number;
number;
}'. }'.
While sometimes that implies a trade-off in what you can express, the intent is to catch legitimate
bugs in our programs. And TypeScript catches a lot of legitimate bugs.
uncalled functions,
function flipCoin() {
// Meant to be Math.random()
return Math.random < 0.5;
Operator
Operator '<'
'<' cannot
cannot be
be applied
applied to
to types
types '()
'() =>
=> number'
number' and
and 'number'.
'number'.
This
This condition
condition will
will always
always return
return 'false'
'false' since
since the
the types
types '"a"'
'"a"' and
and '"b"'
'"b"'
have nohave
overlap.
no overlap.
// Oops, unreachable
}
The type-checker has information to check things like whether we're accessing the right properties
on variables and other properties. Once it has that information, it can also start suggesting which
properties you might want to use.
That means TypeScript can be leveraged for editing code too, and the core type-checker can
provide error messages and code completion as you type in the editor. That's part of what people
often refer to when they talk about tooling in TypeScript.
sendfile
app.listen(3000);
sendFile
sendStatus
TypeScript takes tooling seriously, and that goes beyond completions and errors as you type. An
editor that supports TypeScript can deliver "quick fixes" to automatically fix errors, refactorings to
easily re-organize code, and useful navigation features for jumping to definitions of a variable, or
finding all references to a given variable. All of this is built on top of the type-checker and is fully
cross-platform, so it's likely that your favorite editor has TypeScript support available.
tsc, the TypeScript compiler
We've been talking about type-checking, but we haven't yet used our type-checker. Let's get
acquainted with our new friend tsc , the TypeScript compiler. First we'll need to grab it via npm.
This installs the TypeScript Compiler tsc globally. You can use npx or similar tools if you'd prefer to
run tsc from a local node_modules package instead.
Now let's move to an empty folder and try writing our first TypeScript program: hello.ts :
Notice there are no frills here; this "hello world" program looks identical to what you'd write for a
"hello world" program in JavaScript. And now let's type-check it by running the command tsc
which was installed for us by the typescript package.
tsc hello.ts
Tada!
Wait, "tada" what exactly? We ran tsc and nothing happened! Well, there were no type errors, so
we didn't get any output in our console since there was nothing to report.
But check again - we got some file output instead. If we look in our current directory, we'll see a
hello.js file next to hello.ts . That's the output from our hello.ts file after tsc
compiles or transforms it into a plain JavaScript file. And if we check the contents, we'll see what
TypeScript spits out after it processes a .ts file:
greet("Brendan");
If we run tsc hello.ts again, notice that we get an error on the command line!
TypeScript is telling us we forgot to pass an argument to the greet function, and rightfully so. So
far we've only written standard JavaScript, and yet type-checking was still able to find problems
with our code. Thanks TypeScript!
One thing you might not have noticed from the last example was that our hello.js file changed
again. If we open that file up then we'll see that the contents still basically look the same as our
input file. That might be a bit surprising given the fact that tsc reported an error about our code,
but this is based on one of TypeScript's core values: much of the time, you will know better than
TypeScript.
To reiterate from earlier, type-checking code limits the sorts of programs you can run, and so there's
a tradeoff on what sorts of things a type-checker finds acceptable. Most of the time that's okay, but
there are scenarios where those checks get in the way. For example, imagine yourself migrating
JavaScript code over to TypeScript and introducing type-checking errors. Eventually you'll get
around to cleaning things up for the type-checker, but that original JavaScript code was already
working! Why should converting it over to TypeScript stop you from running it?
So TypeScript doesn't get in your way. Of course, over time, you may want to be a bit more
defensive against mistakes, and make TypeScript act a bit more strictly. In that case, you can use the
--noEmitOnError compiler option. Try changing your hello.ts file and running tsc with
that flag:
Explicit Types
Up until now, we haven't told TypeScript what person or date are. Let's edit the code to tell
TypeScript that person is a string , and that date should be a Date object. We'll also use
the toDateString() method on date .
What we did was add type annotations on person and date to describe what types of values
greet can be called with. You can read that signature as " greet takes a person of type
string , and a date of type Date ".
With this, TypeScript can tell us about other cases where we might have been called incorrectly. For
example...
greet("Maddison", Date());
Argument
Argument of
of type
type 'string'
'string' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type 'Date'.
'Date'.
Keep in mind, we don't always have to write explicit type annotations. In many cases, TypeScript can
even just infer (or "figure out") the types for us even if we omit them.
Even though we didn't tell TypeScript that msg had the type string it was able to figure that
out. That's a feature, and it's best not to add annotations when the type system would end up
inferring the same type anyway.
it means that we're highlighting what your editor would show you inline. You can get the same experience
in the web browser by hovering your mouse over blue-tinted code samples.
Erased Types
Let's take a look at what happens when we compile the above function greet with tsc to
output JavaScript:
"use strict";
function greet(person, date) {
console.log("Hello " + person + ", today is " + date.toDateString() +
}
greet("Maddison", new Date());
More on that second point later, but let's now focus on that first point. Type annotations aren't part
of JavaScript (or ECMAScript to be pedantic), so there really aren't any browsers or other runtimes
that can just run TypeScript unmodified. That's why TypeScript needs a compiler in the first place -
it needs some way to strip out or transform any TypeScript-specific code so that you can run it.
Most TypeScript-specific code gets erased away, and likewise, here our type annotations were
completely erased.
Remember: Type annotations never change the runtime behavior of your program.
Downleveling
One other difference from the above was that our template string was rewritten from
to
Template strings are a feature from a version of ECMAScript called ECMAScript 2015 (a.k.a.
ECMAScript 6, ES2015, ES6, etc. - don't ask). TypeScript has the ability to rewrite code from newer
versions of ECMAScript to older ones such as ECMAScript 3 or ECMAScript 5 (a.k.a. ES3 and ES5).
This process of moving from a newer or "higher" version of ECMAScript down to an older or
"lower" one is sometimes called downleveling.
By default TypeScript targets ES3, an extremely old version of ECMAScript. We could have chosen
something a little bit more recent by using the --target flag. Running with --target
es2015 changes TypeScript to target ECMAScript 2015, meaning code should be able to run
wherever ECMAScript 2015 is supported. So running tsc --target es2015 input.ts gives
us the following output:
While the default target is ES3, the great majority of current browsers support ES2015. Most developers
can therefore safely specify ES2015 or above as a target, unless compatibility with certain ancient
browsers is important.
Strictness
Different users come to TypeScript looking for different things in a type-checker. Some people are
looking for a more loose opt-in experience which can help validate only some parts of their
program, and still have decent tooling. This is the default experience with TypeScript, where types
are optional, inference takes the most lenient types, and there's no checking for potentially
null / undefined values. Much like how tsc emits in the face of errors, these defaults are put
in place to stay out of your way. If you're migrating existing JavaScript, that might be a desirable
first step.
In contrast, a lot of users prefer to have TypeScript validate as much as it can straight away, and
that's why the language provides strictness settings as well. These strictness settings turn static
type-checking from a switch (either your code is checked or not) into something closer to a dial. The
further you turn this dial up, the more TypeScript will check for you. This can require a little extra
work, but generally speaking it pays for itself in the long run, and enables more thorough checks
and more accurate tooling. When possible, a new codebase should always turn these strictness
checks on.
TypeScript has several type-checking strictness flags that can be turned on or off, and all of our
examples will be written with all of them enabled unless otherwise stated. The --strict flag in
the CLI, or "strict": true in a tsconfig.json toggles them all on simultaneously, but we
can opt out of them individually. The two biggest ones you should know about are
noImplicitAny and strictNullChecks .
noImplicitAny
Recall that in some places, TypeScript doesn't try to infer any types for us and instead falls back to
the most lenient type: any . This isn't the worst thing that can happen - after all, falling back to
any is just the plain JavaScript experience anyway.
However, using any often defeats the purpose of using TypeScript in the first place. The more
typed your program is, the more validation and tooling you'll get, meaning you'll run into fewer
bugs as you code. Turning on the noImplicitAny flag will issue an error on any variables whose
type is implicitly inferred as any .
strictNullChecks
By default, values like null and undefined are assignable to any other type. This can make
writing some code easier, but forgetting to handle null and undefined is the cause of
countless bugs in the world - some consider it a billion dollar mistake! The strictNullChecks
flag makes handling null and undefined more explicit, and spares us from worrying about
whether we forgot to handle null and undefined .
Everyday Types
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code,
and explain the corresponding ways to describe those types in TypeScript. This isn't an exhaustive
list, and future chapters will describe more ways to name and use other types.
Types can also appear in many more places than just type annotations. As we learn about the types
themselves, we'll also learn about the places where we can refer to these types to form new
constructs.
We'll start by reviewing the most basic and common types you might encounter when writing
JavaScript or TypeScript code. These will later form the core building blocks of more complex types.
number is for numbers like 42 . JavaScript does not have a special runtime value for integers,
so there's no equivalent to int or float - everything is simply number
The type names String , Number , and Boolean (starting with capital letters) are legal, but refer to
some special built-in types that will very rarely appear in your code. Always use string , number , or
boolean for types.
Arrays
To specify the type of an array like [1, 2, 3] , you can use the syntax number[] ; this syntax
works for any type (e.g. string[] is an array of strings, and so on). You may also see this written
as Array<number> , which means the same thing. We'll learn more about the syntax T<U>
when we cover generics.
Note that [number] is a different thing; refer to the section on tuple types.
any
TypeScript also has a special type, any , that you can use whenever you don't want a particular
value to cause typechecking errors.
When a value is of type any , you can access any properties of it (which will in turn be of type
any ), call it like a function, assign it to (or from) a value of any type, or pretty much anything else
that's syntactically legal:
The any type is useful when you don't want to write out a long type just to convince TypeScript
that a particular line of code is okay.
noImplicitAny
When you don't specify a type, and TypeScript can't infer it from context, the compiler will typically
default to any .
You usually want to avoid this, though, because any isn't type-checked. Use the compiler flag
noImplicitAny to flag any implicit any as an error.
In most cases, though, this isn't needed. Wherever possible, TypeScript tries to automatically infer
the types in your code. For example, the type of a variable is inferred based on the type of its
initializer:
For the most part you don't need to explicitly learn the rules of inference. If you're starting out, try
using fewer type annotations than you think - you might be surprised how few you need for
TypeScript to fully understand what's going on.
Functions
Functions are the primary means of passing data around in JavaScript. TypeScript allows you to
specify the types of both the input and output values of functions.
When you declare a function, you can add type annotations after each parameter to declare what
types of parameters the function accepts. Parameter type annotations go after the parameter name:
When a parameter has a type annotation, arguments to that function will be checked:
Argument
Argument of
of type
type 'number'
'number' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type 'string'.
'string'.
Even if you don't have type annotations on your parameters, TypeScript will still check that you passed the
right number of arguments.
You can also add return type annotations. Return type annotations appear after the parameter list:
Much like variable type annotations, you usually don't need a return type annotation because
TypeScript will infer the function's return type based on its return statements. The type
annotation in the above example doesn't change anything. Some codebases will explicitly specify a
return type for documentation purposes, to prevent accidental changes, or just for personal
preference.
Anonymous Functions
Anonymous functions are a little bit different from function declarations. When a function appears
in a place where TypeScript can determine how it's going to be called, the parameters of that
function are automatically given types.
Here's an example:
// No type annotations here, but TypeScript can spot the bug
const names = ["Alice", "Bob", "Eve"];
Property
Property 'toUppercase'
'toUppercase' does
does not
not exist
exist on
on type
type 'string'.
'string'. Did
Did you
you mean
mean
'toUpperCase'?
'toUpperCase'?
});
Property
Property 'toUppercase'
'toUppercase' does
does not
not exist
exist on
on type
type 'string'.
'string'. Did
Did you
you mean
mean
'toUpperCase'?
'toUpperCase'?
});
Even though the parameter s didn't have a type annotation, TypeScript used the types of the
forEach function, along with the inferred type of the array, to determine the type s will have.
This process is called contextual typing because the context that the function occurred in informed
what type it should have. Similar to the inference rules, you don't need to explicitly learn how this
happens, but understanding that it does happen can help you notice when type annotations aren't
needed. Later, we'll see more examples of how the context that a value occurs in can affect its type.
Object Types
Apart from primitives, the most common sort of type you'll encounter is an object type. This refers
to any JavaScript value with properties, which is almost all of them! To define an object type, we
simply list its properties and their types.
Here, we annotated the parameter with a type with two properties - x and y - which are both of
type number . You can use , or ; to separate the properties, and the last separator is optional
either way.
The type part of each property is also optional. If you don't specify a type, it will be assumed to be
any .
Optional Properties
Object types can also specify that some or all of their properties are optional. To do this, add a ?
after the property name:
In JavaScript, if you access a property that doesn't exist, you'll get the value undefined rather
than a runtime error. Because of this, when you read from an optional property, you'll have to check
for undefined before using it.
function printName(obj: { first: string; last?: string }) {
// Error - might crash if 'obj.last' wasn't provided!
console.log(obj.last.toUpperCase());
Object
Object is
is possibly
possibly 'undefined'.
'undefined'.
Union Types
TypeScript's type system allows you to build new types out of existing ones using a large variety of
operators. Now that we know how to write a few types, it's time to start combining them in
interesting ways.
The first way to combine types you might see is a union type. A union type is type formed from two
or more other types, representing values that may be any one of those types. We refer to each of
these types as the union's members.
Argument
Argument of
of type
type '{
'{ myID:
myID: number;
number; }'
}' is
is not
not assignable
assignable to
to parameter
parameter of
of
type
type 'string
'string || number'.
number'.
Type
Type '{
'{ myID:
myID: number;
number; }'
}' is
is not
not assignable
assignable to
to type
type 'number'.
'number'.
It's easy to provide a value matching a union type - simply provide a type matching any of the
union's members. If you have a value of a union type, how do you work with it?
TypeScript will only allow you to do things with the union if that thing is valid for every member of
the union. For example, if you have the union string | number , you can't use methods that are
only available on string :
Property
Property 'toUpperCase'
'toUpperCase' does
does not
not exist
exist on
on type
type 'string
'string || number'.
number'.
Property
Property 'toUpperCase'
'toUpperCase' does
does not
not exist
exist on
on type
type 'number'.
'number'.
The solution is to narrow the union with code, the same as you would in JavaScript without type
annotations. Narrowing occurs when TypeScript can deduce a more specific type for a value based
on the structure of the code.
For example, TypeScript knows that only a string value will have a typeof value "string" :
function printId(id: number | string) {
if (typeof id === "string") {
// In this branch, id is of type 'string'
console.log(id.toUpperCase());
} else {
// Here, id is of type 'number'
console.log(id);
}
}
Notice that in the else branch, we don't need to do anything special - if x wasn't a string[] ,
then it must have been a string .
Sometimes you'll have a union where all the members have something in common. For example,
both arrays and strings have a slice method. If every member in a union has a property in
common, you can use that property without narrowing:
It might be confusing that a union of types appears to have the intersection of those types' properties. This
is not an accident - the name union comes from type theory. The union number | string is composed
by taking the union of the values from each type. Notice that given two sets with corresponding facts
about each set, only the intersection of those facts applies to the union of the sets themselves. For
example, if we had a room of tall people wearing hats, and another room of Spanish speakers wearings
hats, after combining those rooms, the only thing we know about every person is that they must be
wearing a hat.
Type Aliases
We've been using object types and union types by writing them directly in type annotations. This is
convenient, but it's common to want to use the same type more than once and refer to it by a single
name.
A type alias is exactly that - a name for any type. The syntax for a type alias is:
type Point = {
x: number;
y: number;
};
You can actually use a type alias to give a name to any type at all, not just an object type. For
example, a type alias can name a union type:
Note that aliases are only aliases - you cannot use type aliases to create different/distinct "versions"
of the same type. When you use the alias, it's exactly as if you had written the aliased type. In other
words, this code might look illegal, but is OK according to TypeScript because both types are aliases
for the same type:
type UserInputSanitizedString = string;
Interfaces
An interface declaration is another way to name an object type:
interface Point {
x: number;
y: number;
}
Just like when we used a type alias above, the example works just as if we had used an anonymous
object type. TypeScript is only concerned with the structure of the value we passed to
printCoord - it only cares that it has the expected properties. Being concerned only with the
structure and capabilities of types is why we call TypeScript a structurally typed type system.
Type aliases and interfaces are very similar, and in many cases you can choose between them freely.
Almost all features of an interface are available in type , the key distinction is that a type
cannot be re-opened to add new properties vs an interface which is always extendable.
Interface Type
Adding new fields to an existing interface A type cannot be changed after being created
You'll learn more about these concepts in later chapters, so don't worry if you don't understand all
of these right away.
Prior to TypeScript version 4.2, type alias names may appear in error messages, sometimes in
place of the equivalent anonymous type (which may or may not be desirable). Interfaces will
always be named in error messages.
Type aliases may not participate in declaration merging, but interfaces can.
Interfaces may only be used to declare the shapes of object, not re-name primitives.
Interface names will always appear in their original form in error messages, but only when they
are used by name.
For the most part, you can choose based on personal preference, and TypeScript will tell you if it
needs something to be the other kind of declaration. If you would like a heuristic, use interface
until you need to use features from type .
Type Assertions
Sometimes you will have information about the type of a value that TypeScript can't know about.
For example, if you're using document.getElementById , TypeScript only knows that this will
return some kind of HTMLElement , but you might know that your page will always have an
HTMLCanvasElement with a given ID.
In this situation, you can use a type assertion to specify a more specific type:
Like a type annotation, type assertions are removed by the compiler and won't affect the runtime
behavior of your code.
You can also use the angle-bracket syntax (except if the code is in a .tsx file), which is equivalent:
Reminder: Because type assertions are removed at compile-time, there is no runtime checking associated
with a type assertion. There won't be an exception or null generated if the type assertion is wrong.
TypeScript only allows type assertions which convert to a more specific or less specific version of a
type. This rule prevents "impossible" coercions like:
const x = "hello" as number;
Conversion
Conversion of
of type
type 'string'
'string' to
to type
type 'number'
'number' may
may be
be aa mistake
mistake because
because
neither
neither type
type sufficiently
sufficiently overlaps
overlaps with
with the
the other.
other. If
If this
this was
was
intentional,
intentional, convert
convert the
the expression
expression to
to 'unknown'
'unknown' first.
first.
Sometimes this rule can be too conservative and will disallow more complex coercions that might
be valid. If this happens, you can use two assertions, first to any (or unknown , which we'll
introduce later), then to the desired type:
Literal Types
In addition to the general types string and number , we can refer to specific strings and
numbers in type positions.
One way to think about this is to consider how JavaScript comes with different ways to declare a
variable. Both var and let allow for changing what is held inside the variable, and const does
not. This is reflected in how TypeScript creates types for literals.
Type
Type '"howdy"'
'"howdy"' is
is not
not assignable
assignable to
to type
type '"hello"'.
'"hello"'.
It's not much use to have a variable that can only have one value!
But by combining literals into unions, you can express a much more useful concept - for example,
functions that only accept a certain set of known values:
Argument
Argument of
of type
type '"centre"'
'"centre"' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type '"left"
'"left"
| "right"| "right"
| "center"'.
| "center"'.
Argument
Argument of
of type
type '"automatic"'
'"automatic"' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type
'Options
'Options || "auto"'.
"auto"'.
There's one more kind of literal type: boolean literals. There are only two boolean literal types, and
as you might guess, they are the types true and false . The type boolean itself is actually just
an alias for the union true | false .
Literal Inference
When you initialize a variable with an object, TypeScript assumes that the properties of that object
might change values later. For example, if you wrote code like this:
TypeScript doesn't assume the assignment of 1 to a field which previously had 0 is an error.
Another way of saying this is that obj.counter must have the type number , not 0 , because
types are used to determine both reading and writing behavior.
Argument
Argument of
of type
type 'string'
'string' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type '"GET"
'"GET" |
|"POST"'.
"POST"'.
In the above example req.method is inferred to be string , not "GET" . Because code can be
evaluated between the creation of req and the call of handleRequest which could assign a
new string like "GUESS" to req.method , TypeScript considers this code to have an error.
1. You can change the inference by adding a type assertion in either location:
// Change 1:
const req = { url: "https://example.com", method: "GET" as "GET" };
// Change 2
handleRequest(req.url, req.method as "GET");
Change 1 means "I intend for req.method to always have the literal type "GET" ",
preventing the possible assignment of "GUESS" to that field after. Change 2 means "I know
for other reasons that req.method has the value "GET" ".
2. You can use as const to convert the entire object to be type literals:
The as const prefix acts like const but for the type system, ensuring that all properties are
assigned the literal type instead of a more general version like string or number .
nullandundefined
JavaScript has two primitive values used to signal absent or uninitialized value: null and
undefined .
TypeScript has two corresponding types by the same names. How these types behave depends on
whether you have the strictNullChecks option on.
strictNullChecksoff
With strictNullChecks off, values that might be null or undefined can still be accessed
normally, and the values null and undefined can be assigned to a property of any type. This is
similar to how languages without null checks (e.g. C#, Java) behave. The lack of checking for these
values tends to be a major source of bugs; we always recommend people turn
strictNullChecks on if it's practical to do so in their codebase.
strictNullCheckson
With strictNullChecks on, when a value is null or undefined , you will need to test for
those values before using methods or properties on that value. Just like checking for undefined
before using an optional property, we can use narrowing to check for values that might be null :
TypeScript also has a special syntax for removing null and undefined from a type without
doing any explicit checking. Writing ! after any expression is effectively a type assertion that the
value isn't null or undefined :
Just like other type assertions, this doesn't change the runtime behavior of your code, so it's
important to only use ! when you know that the value can't be null or undefined .
Enums
Enums are a feature added to JavaScript by TypeScript which allows for describing a value which
could be one of a set of possible named constants. Unlike most TypeScript features, this is not a
type-level addition to JavaScript but something added to the language and runtime. Because of this,
it's a feature which you should know exists, but maybe hold off on using unless you are sure. You
can read more about enums in the Enum reference page.
Less Common Primitives
It's worth mentioning the rest of the primitives in JavaScript which are represented in the type
system. Though we will not go into depth here.
bigint
From ES2020 onwards, there is a primitive in JavaScript used for very large integers, BigInt :
You can learn more about BigInt in the TypeScript 3.2 release notes.
symbol
There is a primitive in JavaScript used to create a globally unique reference via the function
Symbol() :
This
This condition
condition will
will always
always return
return 'false'
'false' since
since the
the types
types 'typeof
'typeof
firstName'
firstName' and
and 'typeof
'typeof secondName'
secondName' have
have no
no overlap.
overlap.
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 .
Operator
Operator '+'
'+' cannot
cannot be
be applied
applied to
to types
types 'string
'string || number'
number' and
and 'number'.
'number'.
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.
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.
}
return padding + input;
// ^ = (parameter) padding: string
typeoftype 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:
Object
Object is
is possibly
possibly 'null'.
'null'.
console.log(s);
}
} else if (typeof strs === "string") {
console.log(strs);
} else {
// do nothing
}
}
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 .
In JavaScript, constructs like if 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
NaN
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.
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);
}
}
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:
Keep in mind though that truthiness checking on primitives can often be error prone. As an
example, consider a different attempt at writing printAll
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);
}
}
Equality narrowing
TypeScript also uses switch statements and equality checks like === , !== , == , and != to
narrow types. For example:
y.toLowerCase();
// ^ = (method) String.toLowerCase(): string
} else {
console.log(x);
// ^ = (parameter) x: string | number
console.log(y);
// ^ = (parameter) y: string | boolean
}
}
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 .
console.log(s);
}
} else if (typeof strs === "string") {
console.log(strs);
// ^ = (parameter) strs: string
}
}
}
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;
}
instanceofnarrowing
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.
} else {
console.log(x.toUpperCase());
// ^ = (parameter) x: string
}
}
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.
x = 1;
console.log(x);
// ^ = let x: number
x = "goodbye!";
console.log(x);
// ^ = let x: string
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.
x = 1;
console.log(x);
// ^ = let x: number
x = true;
Type
Type 'boolean'
'boolean' is
is not
not assignable
assignable to
to type
type 'string
'string || number'.
number'.
console.log(x);
// ^ = let x: string | number
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 example() {
let x: string | number | boolean;
console.log(x);
// ^ = let x: boolean
} else {
x = 100;
console.log(x);
// ^ = let x: number
return x;
// ^ = let x: string | number
To define a user-defined type guard, we simply need to define a function whose return type is a type
predicate:
pet is Fish is our type predicate in this example. A predicate takes the form
parameterName is Type , where parameterName must be the name of a parameter from
the current function signature.
Any time isFish is called with some variable, TypeScript will narrow that variable to that specific
type if the original type is compatible.
if (isFish(pet)) {
pet.swim();
} else {
pet.fly();
}
Notice that TypeScript not only knows that pet is a Fish in the if branch; it also knows that in
the else branch, you don't have a Fish , so you must have a Bird .
You may use the type guard isFish to filter an array of Fish | Bird and obtain an array of
Fish :
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;
}
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.
This
This condition
condition will
will always
always return
return 'false'
'false' since
since the
the types
types '"circle"
'"circle" ||
"square"'
"square"' and
and '"rect"'
'"rect"' have
have no
no overlap.
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.
Object
Object is
is possibly
possibly 'undefined'.
'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.radius ** 2;
Object
Object is
is possibly
possibly 'undefined'.
'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.
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;
}
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 .
Property
Property 'radius'
'radius' does
does not
not exist
exist on
on type
type 'Shape'.
'Shape'.
Property
Property 'radius'
'radius' does
does not
not exist
exist on
on type
type 'Square'.
'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 .
}
}
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.
case "square":
return shape.sideLength ** 2;
// ^ = (parameter) shape: Square
}
}
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.
Thenevertype
When narrowing, you can reduce the options of a union to a point where you have removed all
possibilities and have nothing left. In those cases, TypeScript will use a never type to represent a
state which shouldn't exist.
Exhaustiveness checking
The never type is assignable to every type; however, no type is assignable to never (except
never itself ). This means you can use narrowing and rely on never turning up to do exhaustive
checking in a switch statement.
For example, adding a default to our getArea function which tries to assign the shape to
never will raise when every possible case has not been handled.
Adding a new member to the Shape union, will cause a TypeScript error:
interface Triangle {
kind: "triangle";
sideLength: number;
}
Type
Type 'Triangle'
'Triangle' is
is not
not assignable
assignable to
to type
type 'never'.
'never'.
return _exhaustiveCheck;
}
}
More on Functions
Functions are the basic building block of any application, whether they're local functions, imported
from another module, or methods on a class. They're also values, and just like other values,
TypeScript has many ways to describe how functions can be called. Let's learn about how to write
types that describe functions.
greeter(printToConsole);
The syntax (a: string) => void means "a function with one parameter, named a , of type
string, that doesn't have a return value". Just like with function declarations, if a parameter type isn't
specified, it's implicitly any .
Note that the parameter name is required. The function type (string) => void means "a function
with a parameter named string of type any "!
type DescribableFunction = {
description: string;
(someArg: number): boolean;
};
function doSomething(fn: DescribableFunction) {
console.log(fn.description + " returned " + fn(6));
}
Note that the syntax is slightly different compared to a function type expression - use : between
the parameter list and the return type rather than => .
Construct Signatures
JavaScript functions can also be invoked with the new operator. TypeScript refers to these as
constructors because they usually create a new object. You can write a construct signature by
adding the new keyword in front of a call signature:
type SomeConstructor = {
new (s: string): SomeObject;
};
function fn(ctor: SomeConstructor) {
return new ctor("hello");
}
Some objects, like JavaScript's Date object, can be called with or without new . You can combine
call and construct signatures in the same type arbitrarily:
interface CallOrConstruct {
new (s: string): Date;
(n?: number): number;
}
Generic Functions
It's common to write a function where the types of the input relate to the type of the output, or
where the types of two inputs are related in some way. Let's consider for a moment a function that
returns the first element of an array:
This function does its job, but unfortunately has the return type any . It'd be better if the function
returned the type of the array element.
In TypeScript, generics are used when we want to describe a correspondence between two values.
We do this by declaring a type parameter in the function signature:
By adding a type parameter Type to this function and using it in two places, we've created a link
between the input of the function (the array) and the output (the return value). Now when we call it,
a more specific type comes out:
// s is of type 'string'
const s = firstElement(["a", "b", "c"]);
// n is of type 'number'
const n = firstElement([1, 2, 3]);
Inference
Note that we didn't have to specify Type in this sample. The type was inferred - chosen
automatically - by TypeScript.
We can use multiple type parameters as well. For example, a standalone version of map would
look like this:
function map<Input, Output>(arr: Input[], func: (arg: Input) => Output): O
return arr.map(func);
}
Note that in this example, TypeScript could infer both the type of the Input type parameter (from
the given string array), as well as the Output type parameter based on the return value of the
function expression ( number ).
Constraints
We've written some generic functions that can work on any kind of value. Sometimes we want to
relate two values, but can only operate on a certain subset of values. In this case, we can use a
constraint to limit the kinds of types that a type parameter can accept.
Let's write a function that returns the longer of two values. To do this, we need a length property
that's a number. We constrain the type parameter to that type by writing an extends clause:
Argument
Argument of
of type
type 'number'
'number' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type '{
'{
length:
length: number;
number; }'.
}'.
There are few interesting things to note in this example. We allowed TypeScript to infer the return
type of longest . Return type inference also works on generic functions.
The types of longerArray and longerString were inferred based on the arguments.
Remember, generics are all about relating two or more values with the same type!
Finally, just as we'd like, the call to longest(10, 100) is rejected because the number type
doesn't have a .length property.
Type
Type '{'{ length:
length: number;
number; }'
}' is
is not
not assignable
assignable to
to type
type 'Type'.
'Type'.
'{
'{ length:
length: number;
number; }'
}' is
is assignable
assignable toto the
the constraint
constraint of
of type
type 'Type',
'Type',
but
but 'Type'
'Type' could
could be
be instantiated
instantiated with
with aa different
different subtype
subtype of
of constraint
constraint '{
'{
length:
length:
number;
number;
}'.}'.
}
}
It might look like this function is OK - Type is constrained to { length: number } , and the
function either returns Type or a value matching that constraint. The problem is that the function
promises to return the same kind of object as was passed in, not just some object matching the
constraint. If this code were legal, you could write code that definitely wouldn't work:
// 'arr' gets value { length: 6 }
const arr = minimumLength([1, 2, 3], 6);
// and crashes here because arrays have
// a 'slice' method, but not the returned object!
console.log(arr.slice(0));
TypeScript can usually infer the intended type arguments in a generic call, but not always. For
example, let's say you wrote a function to combine two arrays:
Type
Type 'string'
'string' is
is not
not assignable
assignable to
to type
type 'number'.
'number'.
Writing generic functions is fun, and it can be easy to get carried away with type parameters.
Having too many type parameters or using constraints where they aren't needed can make
inference less successful, frustrating callers of your function.
// a: number (good)
const a = firstElement1([1, 2, 3]);
// b: any (bad)
const b = firstElement2([1, 2, 3]);
These might seem identical at first glance, but firstElement1 is a much better way to write this
function. Its inferred return type is Type , but firstElement2 's inferred return type is any
because TypeScript has to resolve the arr[0] expression using the constraint type, rather than
"waiting" to resolve the element during a call.
Rule: When possible, use the type parameter itself rather than constraining it
We've created a type parameter Func that doesn't relate two values. That's always a red flag,
because it means callers wanting to specify type arguments have to manually specify an extra type
argument for no reason. Func doesn't do anything but make the function harder to read and
reason about!
greet("world");
Remember, type parameters are for relating the types of multiple values. If a type parameter is only
used once in the function signature, it's not relating anything.
Rule: If a type parameter only appears in one location, strongly reconsider if you actually need it
Optional Parameters
Functions in JavaScript often take a variable number of arguments. For example, the toFixed
method of number takes an optional digit count:
Although the parameter is specified as type number , the x parameter will actually have the type
number | undefined because unspecified parameters in JavaScript get the value
undefined .
Now in the body of f , x will have type number because any undefined argument will be
replaced with 10 . Note that when a parameter is optional, callers can always pass undefined , as
this simply simulates a "missing" argument:
Once you've learned about optional parameters and function type expressions, it's very easy to
make the following mistakes when writing functions that invoke callbacks:
function myForEach(arr: any[], callback: (arg: any, index?: number) => voi
for (let i = 0; i < arr.length; i++) {
callback(arr[i], i);
}
}
What people usually intend when writing index? as an optional parameter is that they want both
of these calls to be legal:
What this actually means is that callback might get invoked with one argument. In other words,
the function definition says that the implementation might look like this:
function myForEach(arr: any[], callback: (arg: any, index?: number) => voi
for (let i = 0; i < arr.length; i++) {
// I don't feel like providing the index today
callback(arr[i]);
}
}
In turn, TypeScript will enforce this meaning and issue errors that aren't really possible:
Object
Object is
is possibly
possibly 'undefined'.
'undefined'.
});
In JavaScript, if you call a function with more arguments than there are parameters, the extra
arguments are simply ignored. TypeScript behaves the same way. Functions with fewer parameters
(of the same types) can always take the place of functions with more parameters.
When writing a function type for a callback, never write an optional parameter unless you intend to call the
function without passing that argument
Function Overloads
Some JavaScript functions can be called in a variety of argument counts and types. For example,
you might write a function to produce a Date that takes either a timestamp (one argument) or a
month/day/year specification (three arguments).
In TypeScript, we can specify a function that can be called in different ways by writing overload
signatures. To do this, write some number of function signatures (usually two or more), followed by
the body of the function:
No
No overload
overload expects
expects 22 arguments,
arguments, but
but overloads
overloads do
do exist
exist that
that expect
expect either
either
1 or 3 1arguments.
or 3 arguments.
In this example, we wrote two overloads: one accepting one argument, and another accepting three
arguments. These first two signatures are called the overload signatures.
Expected
Expected 11 arguments,
arguments, but
but got
got 0.
0.
Again, the signature used to write the function body can't be "seen" from the outside.
The signature of the implementation is not visible from the outside. When writing an overloaded function,
you should always have two or more signatures above the implementation of the function.
The implementation signature must also be compatible with the overload signatures. For example,
these functions have errors because the implementation signature doesn't match the overloads in a
correct way:
This
This overload
overload signature
signature is
is not
not compatible
compatible with
with its
its implementation
implementation
signature.
signature.
This
This overload
overload signature
signature is
is not
not compatible
compatible with
with its
its implementation
implementation
signature.
signature.
Like generics, there are a few guidelines you should follow when using function overloads.
Following these principles will make your function easier to call, easier to understand, and easier to
implement.
This function is fine; we can invoke it with strings or arrays. However, we can't invoke it with a value
that might be a string or an array, because TypeScript can only resolve a function call to a single
overload:
len(""); // OK
len([0]); // OK
len(Math.random() > 0.5 ? "hello" : [0]);
No
No overload
overload matches
matches this
this call.
call.
Overload
Overload 11 of
of 2,
2, '(s:
'(s: string):
string): number',
number', gave
gave the
the following
following error.
error.
Argument
Argument of
of type
type 'number[]
'number[] || "hello"'
"hello"' is
is not
not assignable
assignable to
to parameter
parameter
of type 'string'.
of type 'string'.
Type
Type 'number[]'
'number[]' is
is not
not assignable
assignable to
to type
type 'string'.
'string'.
Overload
Overload 22 of
of 2,
2, '(arr:
'(arr: any[]):
any[]): number',
number', gave
gave the
the following
following error.
error.
Argument
Argument of
of type
type 'number[]
'number[] || "hello"'
"hello"' is
is not
not assignable
assignable to
to parameter
parameter
of type 'any[]'.
of type 'any[]'.
Type
Type 'string'
'string' is
is not
not assignable
assignable to
to type
type 'any[]'.
'any[]'.
Because both overloads have the same argument count and same return type, we can instead write
a non-overloaded version of the function:
Always prefer parameters with union types instead of overloads when possible
Declaringthisin a Function
TypeScript will infer what the this should be in a function via code flow analysis, for example in
the following:
const user = {
id: 123,
admin: false,
becomeAdmin: function () {
this.admin = true;
},
};
TypeScript understands that the function user.becomeAdmin has a corresponding this which
is the outer object user . this , heh, can be enough for a lot of cases, but there are a lot of cases
where you need more control over what object this represents. The JavaScript specification
states that you cannot have a parameter called this , and so TypeScript uses that syntax space to
let you declare the type for this in the function body.
interface DB {
filterUsers(filter: (this: User) => boolean): User[];
}
const db = getDB();
const admins = db.filterUsers(function () {
return this.isAdmin;
});
This pattern is common with callback-style APIs, where another object typically controls when your
function is called. Note that you need to use function and not arrow functions to get this
behavior:
const db = getDB();
const admins = db.filterUsers(() => this.isAdmin);
The
The containing
containing arrow
arrow function
function captures
captures the
the global
global value
value of
of 'this'.
'this'.
Element
Element implicitly
implicitly has
has an
an 'any'
'any' type
type because
because type
type 'typeof
'typeof globalThis'
globalThis' has
has
no index
no index
signature.
signature.
void
void represents the return value of functions which don't return a value. It's the inferred type any
time a function doesn't have any return statements, or doesn't return any explicit value from
those return statements:
In JavaScript, a function that doesn't return any value will implicitly return the value undefined .
However, void and undefined are not the same thing in TypeScript. See the reference page
[[Why void is a special type]] for a longer discussion about this.
object
The special type object refers to any value that isn't a primitive ( string , number , boolean ,
symbol , null , or undefined ). This is different from the empty object type { } , and also
different from the global type Object . You can read the reference page about [[The global types]]
for information on what Object is for - long story short, don't ever use Object .
object is not Object . Always use object !
Note that in JavaScript, function values are objects: They have properties, have
Object.prototype in their prototype chain, are instanceof Object , you can call
Object.keys on them, and so on. For this reason, function types are considered to be object s
in TypeScript.
unknown
The unknown type represents any value. This is similar to the any type, but is safer because it's
not legal to do anything with an unknown value:
Object
Object is
is of
of type
type 'unknown'.
'unknown'.
This is useful when describing function types because you can describe functions that accept any
value without having any values in your function body.
Conversely, you can describe a function that returns a value of unknown type:
never
The never type represents values which are never observed. In a return type, this means that the
function throws an exception or terminates execution of the program.
never also appears when TypeScript determines there's nothing left in a union.
Function
The global type Function describes properties like bind , call , apply , and others present
on all function values in JavaScript. It also has the special property that values of type Function
can always be called; these calls return any :
This is an untyped function call and is generally best avoided because of the unsafe any return
type.
If need to accept an arbitrary function but don't intend to call it, the type () => void is generally
safer.
A rest parameter appears after all other parameters, and uses the ... syntax:
In TypeScript, the type annotation on these parameters is implicitly any[] instead of any , and
any type annotation given must be of the form Array<T> or T[] , or a tuple type (which we'll
learn about later).
Rest Arguments
Conversely, we can provide a variable number of arguments from an array using the spread syntax.
For example, the push method of arrays takes any number of arguments:
Note that in general, TypeScript does not assume that arrays are immutable. This can lead to some
surprising behavior:
Expected
Expected 22 arguments,
arguments, but
but got
got 00 or
or more.
more.
The best fix for this situation depends a bit on your code, but in general a const context is the
most straightforward solution:
Using rest arguments may require turning on downlevelIteration when targeting older
runtimes.
Parameter Destructuring
You can use parameter destructuring to conveniently unpack
Background Reading:
objects provided as an argument into one or more local variables in
Destructuring Assignment
the function body. In JavaScript, it looks like this:
function sum({ a, b, c }) {
console.log(a + b + c);
}
sum({ a: 10, b: 3, c: 9 });
The type annotation for the object goes after the destructuring syntax:
This can look a bit verbose, but you can use a named type here as well:
Return typevoid
The void return type for functions can produce some unusual, but expected behavior.
Contextual typing with a return type of void does not force functions to not return something.
Another way to say this is a contextual function type with a void return type ( type vf = ()
=> void ), when implemented, can return any other value, but it will be ignored.
Thus, the following implementations of the type () => void are valid:
And when the return value of one of these functions is assigned to another variable, it will retain the
type of void :
const v1 = f1();
const v2 = f2();
const v3 = f3();
This behavior exists so that the following code is valid even though Array.prototype.push
returns a number and the Array.prototype.forEach method expects a function with a
return type of void .
const src = [1, 2, 3];
const dst = [0];
There is one other special case to be aware of, when a literal function definition has a void return
type, that function must not return anything.
v1 handbook
v2 handbook
FAQ - "Why are functions returning non-void assignable to function returning void?"
Object Types
In JavaScript, the fundamental way that we group and pass around data is through objects. In
TypeScript, we represent those through object types.
interface Person {
name: string;
age: number;
}
or a type alias.
type Person = {
name: string;
age: number;
};
In all three examples above, we've written functions that take objects that contain the property
name (which must be a string ) and age (which must be a number ).
Property Modifiers
Each property in an object type can specify a couple of things: the type, whether the property is
optional, and whether the property can be written to.
Optional Properties
Much of the time, we'll find ourselves dealing with objects that might have a property set. In those
cases, we can mark those properties as optional by adding a question mark ( ? ) to the end of their
names.
interface PaintOptions {
shape: Shape;
xPos?: number;
yPos?: number;
}
In this example, both xPos and yPos are considered optional. We can choose to provide either
of them, so every call above to paintShape is valid. All optionality really says is that if the
property is set, it better have a specific type.
interface PaintOptions {
shape: Shape;
xPos?: number;
yPos?: number;
}
We can also read from those properties - but when we do under strictNullChecks ,
TypeScript will tell us they're potentially undefined .
// ...
}
In JavaScript, even if the property has never been set, we can still access it - it's just going to give us
the value undefined . We can just handle undefined specially.
// ...
}
Note that this pattern of setting defaults for unspecified values is so common that JavaScript has
syntax to support it.
// ...
}
Here we used a destructuring pattern for paintShape 's parameter, and provided default values
for xPos and yPos . Now xPos and yPos are both definitely present within the body of
paintShape , but optional for any callers to paintShape .
Note that there is currently no way to place type annotations within destructuring patterns. This is because
the following syntax already means something different in JavaScript.
Cannot
Cannot find
find name
name 'shape'.
'shape'. Did
Did you
you mean
mean 'Shape'?
'Shape'?
render(xPos);
Cannot
Cannot find
find name
name 'xPos'.
'xPos'.
In an object destructuring pattern, shape: Shape means "grab the property shape and
redefine it locally as a variable named Shape . Likewise xPos: number creates a variable
named number whose value is based on the parameter's xPos .
readonlyProperties
Properties can also be marked as readonly for TypeScript. While it won't change any behavior at
runtime, a property marked as readonly can't be written to during type-checking.
interface SomeType {
readonly prop: string;
}
Cannot
Cannot assign
assign to
to 'prop'
'prop' because
because it
it is
is aa read-only
read-only property.
property.
Using the readonly modifier doesn't necessarily imply that a value is totally immutable - or in
other words, that its internal contents can't be changed. It just means the property itself can't be re-
written to.
interface Home {
readonly resident: { name: string; age: number };
}
Cannot
Cannot assign
assign to
to 'resident'
'resident' because
because it
it is
is aa read-only
read-only property.
property.
It's important to manage expectations of what readonly implies. It's useful to signal intent
during development time for TypeScript on how an object should be used. TypeScript doesn't factor
in whether properties on two types are readonly when checking whether those types are
compatible, so readonly properties can also change via aliasing.
interface Person {
name: string;
age: number;
}
interface ReadonlyPerson {
readonly name: string;
readonly age: number;
}
// works
let readonlyPerson: ReadonlyPerson = writablePerson;
Extending Types
It's pretty common to have types that might be more specific versions of other types. For example,
we might have a BasicAddress type that describes the fields necessary for sending letters and
packages in the U.S.
interface BasicAddress {
name?: string;
street: string;
city: string;
country: string;
postalCode: string;
}
In some situations that's enough, but addresses often have a unit number associated with them if
the building at an address has multiple units. We can then describe an AddressWithUnit .
interface AddressWithUnit {
name?: string;
unit: string;
street: string;
city: string;
country: string;
postalCode: string;
}
This does the job, but the downside here is that we had to repeat all the other fields from
BasicAddress when our changes were purely additive. Instead, we can extend the original
BasicAddress type and just add the new fields that are unique to AddressWithUnit .
interface BasicAddress {
name?: string;
street: string;
city: string;
country: string;
postalCode: string;
}
The extends keyword on an interface allows us to effectively copy members from other
named types, and add whatever new members we want. This can be useful for cutting down the
amount of type declaration boilerplate we have to write, and for signaling intent that several
different declarations of the same property might be related. For example, AddressWithUnit
didn't need to repeat the street property, and because street originates from
BasicAddress , a reader will know that those two types are related in some way.
interface Circle {
radius: number;
}
Intersection Types
interface s allowed us to build up new types from other types by extending them. TypeScript
provides another construct called intersection types that is mainly used to combine existing object
types.
interface Colorful {
color: string;
}
interface Circle {
radius: number;
}
Here, we've intersected Colorful and Circle to produce a new type that has all the members
of Colorful and Circle .
function draw(circle: Colorful & Circle) {
console.log(`Color was ${circle.color}`);
console.log(`Radius was ${circle.radius}`);
}
// okay
draw({ color: "blue", radius: 42 });
// oops
draw({ color: "red", raidus: 42 });
Argument
Argument ofof type
type '{
'{ color:
color: string;
string; raidus:
raidus: number;
number; }'
}' is
is not
not assignable
assignable to
to
parameter
parameter
of of
typetype
'Colorful
'Colorful
& Circle'.
& Circle'.
Object
Object literal
literal may
may only
only specify
specify known
known properties,
properties, but
but 'raidus'
'raidus' does
does not
not
existexist
in type
in type
'Colorful
'Colorful
& Circle'.
& Circle'.
Did Did
you you
meanmean
to write
to write
'radius'?
'radius'?
interface Box {
contents: any;
}
Right now, the contents property is typed as any , which works, but can lead to accidents down
the line.
We could instead use unknown , but that would mean that in cases where we already know the
type of contents , we'd need to do precautionary checks, or use error-prone type assertions.
interface Box {
contents: unknown;
}
let x: Box = {
contents: "hello world",
};
One type safe approach would be to instead scaffold out different Box types for every type of
contents .
interface NumberBox {
contents: number;
}
interface StringBox {
contents: string;
}
interface BooleanBox {
contents: boolean;
}
But that means we'll have to create different functions, or overloads of functions, to operate on
these types.
function setContents(box: StringBox, newContents: string): void;
function setContents(box: NumberBox, newContents: number): void;
function setContents(box: BooleanBox, newContents: boolean): void;
function setContents(box: { contents: any }, newContents: any) {
box.contents = newContents;
}
That's a lot of boilerplate. Moreover, we might later need to introduce new types and overloads. This
is frustrating, since our box types and overloads are all effectively the same.
Instead, we can make a generic Box type which declares a type parameter.
interface Box<Type> {
contents: Type;
}
You might read this as “A Box of Type is something whose contents have type
Type †. Later on, when we refer to Box , we have to give a type argument in place of Type .
Think of Box as a template for a real type, where Type is a placeholder that will get replaced with
some other type. When TypeScript sees Box<string> , it will replace every instance of Type in
Box<Type> with string , and end up working with something like { contents: string
} . In other words, Box<string> and our earlier StringBox work identically.
interface Box<Type> {
contents: Type;
}
interface StringBox {
contents: string;
}
Box is reusable in that Type can be substituted with anything. That means that when we need a
box for a new type, we don't need to declare a new Box type at all (though we certainly could if we
wanted to).
interface Box<Type> {
contents: Type;
}
interface Apple {
// ....
}
This also means that we can avoid overloads entirely by instead using generic functions.
interface Box<Type> {
contents: Type;
}
type Box<Type> = {
contents: Type;
};
Since type aliases, unlike interfaces, can describe more than just object types, we can also use them
to write other kinds of generic helper types.
TheArrayType
Generic object types are often some sort of container type that work independently of the type of
elements they contain. It's ideal for data structures to work this way so that they're re-usable across
different data types.
It turns out we've been working with a type just like that throughout this handbook: the Array
type. Whenever we write out types like number[] or string[] , that's really just a shorthand
for Array<number> and Array<string> .
Much like the Box type above, Array itself is a generic type.
interface Array<Type> {
/**
* Gets or sets the length of the array.
*/
length: number;
/**
* Removes the last element from an array and returns it.
*/
pop(): Type | undefined;
/**
* Appends new elements to an array, and returns the new length of the a
*/
push(...items: Type[]): number;
// ...
}
Modern JavaScript also provides other data structures which are generic, like Map<K, V> ,
Set<T> , and Promise<T> . All this really means is that because of how Map , Set , and
Promise behave, they can work with any sets of types.
TheReadonlyArrayType
The ReadonlyArray is a special type that describes arrays that shouldn't be changed.
Property
Property 'push'
'push' does
does not
not exist
exist on
on type
type 'readonly
'readonly string[]'.
string[]'.
Much like the readonly modifier for properties, it's mainly a tool we can use for intent. When we
see a function that returns ReadonlyArray s, it tells us we're not meant to change the contents at
all, and when we see a function that consumes ReadonlyArray s, it tells us that we can pass any
array into that function without worrying that it will change its contents.
'ReadonlyArray'
'ReadonlyArray' only
only refers
refers to
to aa type,
type, but
but is
is being
being used
used as
as aa value
value here.
here.
Just as TypeScript provides a shorthand syntax for Array<Type> with Type[] , it also provides
a shorthand syntax for ReadonlyArray<Type> with readonly Type[] .
function doStuff(values: readonly string[]) {
// We can read from 'values'...
const copy = values.slice();
console.log(`The first value is ${values[0]}`);
Property
Property 'push'
'push' does
does not
not exist
exist on
on type
type 'readonly
'readonly string[]'.
string[]'.
One last thing to note is that unlike the readonly property modifier, assignability isn't
bidirectional between regular Array s and ReadonlyArray s.
x = y;
y = x;
The
The type
type 'readonly
'readonly string[]'
string[]' is
is 'readonly'
'readonly' and
and cannot
cannot be
be assigned
assigned to
to the
the
mutable
mutable
typetype
'string[]'.
'string[]'.
Tuple Types
A tuple type is another sort of Array type that knows exactly how many elements it contains, and
exactly which types it contains at specific positions.
const b = pair[1];
// ^ = const b: number
// ...
}
doSomething(["hello", 42]);
const c = pair[2];
Tuple
Tuple type
type '[string,
'[string, number]'
number]' of
of length
length '2'
'2' has
has no
no element
element at
at index
index '2'.
'2'.
console.log(inputString);
// ^ = const inputString: string
console.log(hash);
// ^ = const hash: number
Tuple types are useful in heavily convention-based APIs, where each element's meaning is "obvious". This
gives us flexibility in whatever we want to name our variables when we destructure them. In the above
example, we were able to name elements 0 and 1 to whatever we wanted.
However, since not every user holds the same view of what's obvious, it may be worth reconsidering
whether using objects with descriptive property names may be better for your API.
Other than those length checks, simple tuple types like these are equivalent to types which are
versions of Array s that declare properties for specific indexes, and that declare length with a
numeric literal type.
interface StringNumberPair {
// specialized properties
length: 2;
0: string;
1: number;
Another thing you may be interested in is that tuples can have optional properties by writing out a
question mark ( ? after an element's type). Optional tuple elements can only come at the end, and
also affect the type of length .
Tuples can also have rest elements, which have to be an array/tuple type.
StringBooleansNumber describes a tuple whose first element is string and then any
number of boolean s and ending with a number .
A tuple with a rest element has no set "length" - it only has a set of well-known elements in
different positions.
Why might optional and rest elements be useful? Well, it allows TypeScript to correspond tuples
with parameter lists. Tuples types can be used in rest parameters and arguments, so that the
following:
This is handy when you want to take a variable number of arguments with a rest parameter, and
you need a minimum number of elements, but you don't want to introduce intermediate variables.
readonlyTuple Types
One final note about tuple types - tuples types have readonly variants, and can be specified by
sticking a readonly modifier in front of them - just like with array shorthand syntax.
function doSomething(pair: readonly [string, number]) {
// ...
}
As you might expect, writing to any property of a readonly tuple isn't allowed in TypeScript.
Cannot
Cannot assign
assign to
to '0'
'0' because
because it
it is
is aa read-only
read-only property.
property.
Tuples tend to be created and left un-modified in most code, so annotating types as readonly
tuples when possible is a good default. This is also important given that array literals with const
assertions will be inferred with readonly tuple types.
distanceFromOrigin(point);
Argument
Argument of
of type
type 'readonly
'readonly [3,
[3, 4]'
4]' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type
'[number,
'[number,
number]'.
number]'.
The
The type
type 'readonly
'readonly [3,
[3, 4]'
4]' is
is 'readonly'
'readonly' and
and cannot
cannot be
be assigned
assigned to
to the
the
mutable
mutable
typetype
'[number,
'[number,
number]'.
number]'.
Here, distanceFromOrigin never modifies its elements, but expects a mutable tuple. Since
point 's type was inferred as readonly [3, 4] , it won't be compatible with [number,
number] since that type can't guarantee point 's elements won't be mutated.
Creating Types from Types
TypeScript's type system is very powerful because it allows expressing types in terms of other
types.
The simplest form of this idea is generics, we actually have a wide variety of type operators
available to us. It's also possible to express types in terms of values that we already have.
By combining various type operators, we can express complex operations and values in a succinct,
maintainable way. In this section we'll cover ways to express a new type in terms of an existing type
or value.
Typeof Type Operator - Using the typeof operator to create new types
Conditional Types - Types which act like if statements in the type system
A major part of software engineering is building components that not only have well-defined and
consistent APIs, but are also reusable. Components that are capable of working on the data of today
as well as the data of tomorrow will give you the most flexible capabilities for building up large
software systems.
In languages like C# and Java, one of the main tools in the toolbox for creating reusable
components is generics, that is, being able to create a component that can work over a variety of
types rather than a single one. This allows users to consume these components and use their own
types.
Without generics, we would either have to give the identity function a specific type:
Or, we could describe the identity function using the any type:
While using any is certainly generic in that it will cause the function to accept any and all types for
the type of arg , we actually are losing the information about what that type was when the
function returns. If we passed in a number, the only information we have is that any type could be
returned.
Instead, we need a way of capturing the type of the argument in such a way that we can also use it
to denote what is being returned. Here, we will use a type variable, a special kind of variable that
works on types rather than values.
We've now added a type variable Type to the identity function. This Type allows us to capture
the type the user provides (e.g. number ), so that we can use that information later. Here, we use
Type again as the return type. On inspection, we can now see the same type is used for the
argument and the return type. This allows us to traffic that type information in one side of the
function and out the other.
We say that this version of the identity function is generic, as it works over a range of types.
Unlike using any , it's also just as precise (ie, it doesn't lose any information) as the first
identity function that used numbers for the argument and return type.
Once we've written the generic identity function, we can call it in one of two ways. The first way is to
pass all of the arguments, including the type argument, to the function:
Here we explicitly set Type to be string as one of the arguments to the function call, denoted
using the <> around the arguments rather than () .
The second way is also perhaps the most common. Here we use type argument inference -- that is,
we want the compiler to set the value of Type for us automatically based on the type of the
argument we pass in:
Notice that we didn't have to explicitly pass the type in the angle brackets ( <> ); the compiler just
looked at the value "myString" , and set Type to its type. While type argument inference can
be a helpful tool to keep code shorter and more readable, you may need to explicitly pass in the
type arguments as we did in the previous example when the compiler fails to infer the type, as may
happen in more complex examples.
What if we want to also log the length of the argument arg to the console with each call? We
might be tempted to write this:
Property
Property 'length'
'length' does
does not
not exist
exist on
on type
type 'Type'.
'Type'.
return arg;
}
When we do, the compiler will give us an error that we're using the .length member of arg ,
but nowhere have we said that arg has this member. Remember, we said earlier that these type
variables stand in for any and all types, so someone using this function could have passed in a
number instead, which does not have a .length member.
Let's say that we've actually intended this function to work on arrays of Type rather than Type
directly. Since we're working with arrays, the .length member should be available. We can
describe this just like we would create arrays of other types:
function loggingIdentity<Type>(arg: Type[]): Type[] {
console.log(arg.length);
return arg;
}
You can read the type of loggingIdentity as "the generic function loggingIdentity takes
a type parameter Type , and an argument arg which is an array of Type s, and returns an array
of Type s." If we passed in an array of numbers, we'd get an array of numbers back out, as Type
would bind to number . This allows us to use our generic type variable Type as part of the types
we're working with, rather than the whole type, giving us greater flexibility.
You may already be familiar with this style of type from other languages. In the next section, we'll
cover how you can create your own generic types like Array<Type> .
Generic Types
In previous sections, we created generic identity functions that worked over a range of types. In this
section, we'll explore the type of the functions themselves and how to create generic interfaces.
The type of generic functions is just like those of non-generic functions, with the type parameters
listed first, similarly to function declarations:
We can also write the generic type as a call signature of an object literal type:
Which leads us to writing our first generic interface. Let's take the object literal from the previous
example and move it to an interface:
interface GenericIdentityFn {
<Type>(arg: Type): Type;
}
In a similar example, we may want to move the generic parameter to be a parameter of the whole
interface. This lets us see what type(s) we're generic over (e.g. Dictionary<string> rather than
just Dictionary ). This makes the type parameter visible to all the other members of the
interface.
interface GenericIdentityFn<Type> {
(arg: Type): Type;
}
Notice that our example has changed to be something slightly different. Instead of describing a
generic function, we now have a non-generic function signature that is a part of a generic type.
When we use GenericIdentityFn , we now will also need to specify the corresponding type
argument (here: number ), effectively locking in what the underlying call signature will use.
Understanding when to put the type parameter directly on the call signature and when to put it on
the interface itself will be helpful in describing what aspects of a type are generic.
In addition to generic interfaces, we can also create generic classes. Note that it is not possible to
create generic enums and namespaces.
Generic Classes
A generic class has a similar shape to a generic interface. Generic classes have a generic type
parameter list in angle brackets ( <> ) following the name of the class.
class GenericNumber<NumType> {
zeroValue: NumType;
add: (x: NumType, y: NumType) => NumType;
}
This is a pretty literal use of the GenericNumber class, but you may have noticed that nothing is
restricting it to only use the number type. We could have instead used string or even more
complex objects.
console.log(stringNumeric.add(stringNumeric.zeroValue, "test"));
Just as with interface, putting the type parameter on the class itself lets us make sure all of the
properties of the class are working with the same type.
As we cover in our section on classes, a class has two sides to its type: the static side and the
instance side. Generic classes are only generic over their instance side rather than their static side,
so when working with classes, static members can not use the class's type parameter.
Generic Constraints
If you remember from an earlier example, you may sometimes want to write a generic function that
works on a set of types where you have some knowledge about what capabilities that set of types
will have. In our loggingIdentity example, we wanted to be able to access the .length
property of arg , but the compiler could not prove that every type had a .length property, so it
warns us that we can't make this assumption.
Property
Property 'length'
'length' does
does not
not exist
exist on
on type
type 'Type'.
'Type'.
return arg;
}
Instead of working with any and all types, we'd like to constrain this function to work with any and
all types that also have the .length property. As long as the type has this member, we'll allow
it, but it's required to have at least this member. To do so, we must list our requirement as a
constraint on what Type can be.
To do so, we'll create an interface that describes our constraint. Here, we'll create an interface that
has a single .length property and then we'll use this interface and the extends keyword to
denote our constraint:
interface Lengthwise {
length: number;
}
Because the generic function is now constrained, it will no longer work over any and all types:
loggingIdentity(3);
Argument
Argument of
of type
type 'number'
'number' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type
'Lengthwise'.
'Lengthwise'.
Instead, we need to pass in values whose type has all the required properties:
let x = { a: 1, b: 2, c: 3, d: 4 };
getProperty(x, "a");
getProperty(x, "m");
Argument
Argument ofof type
type '"m"'
'"m"' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type '"a"
'"a" || "b" |
"b"
"c" || "c"
"d"'.| "d"'.
A more advanced example uses the prototype property to infer and constrain relationships between
the constructor function and the instance side of class types.
class BeeKeeper {
hasMask: boolean;
}
class ZooKeeper {
nametag: string;
}
class Animal {
numLegs: number;
}
createInstance(Lion).keeper.nametag;
createInstance(Bee).keeper.hasMask;
Thekeyoftype operator
The keyof operator takes an object type and produces a string or numeric literal union of its keys:
If the type has a string or number index signature, keyof will return those types instead:
Note that in this example, M is string | number -- this is because JavaScript object keys are
always coerced to a string, so obj[0] is always the same as obj["0"] .
keyof types become especially useful when combined with mapped types, which we'll learn more
about later.
Typeof Type Operator
Thetypeoftype operator
JavaScript already has a typeof operator you can use in an expression context:
// Prints "string"
console.log(typeof "Hello world");
TypeScript adds a typeof operator you can use in a type context to refer to the type of a variable
or property:
let s = "hello";
let n: typeof s;
// ^ = let n: string
This isn't very useful for basic types, but combined with other type operators, you can use typeof
to conveniently express many patterns. For an example, let's start by looking at the predefined type
ReturnType<T> . It takes a function type and produces its return type:
'f'
'f' refers
refers to
to aa value,
value, but
but is
is being
being used
used as
as aa type
type here.
here. Did
Did you
you mean
mean
'typeof
'typeof f'?
f'?
Remember that values and types aren't the same thing. To refer to the type that the value f has, we
use typeof :
function f() {
return { x: 10, y: 3 };
}
type P = ReturnType<typeof f>;
// ^ = type P = {
// x: number;
// y: number;
// }
Limitations
TypeScript intentionally limits the sorts of expressions you can use typeof on.
Specifically, it's only legal to use typeof on identifiers (i.e. variable names) or their properties.
This helps avoid the confusing trap of writing code you think is executing, but isn't:
','
',' expected.
expected.
Indexed Access Types
We can use an indexed access type to look up a specific property on another type:
The indexing type is itself a type, so we can use unions, keyof , or other types entirely:
You'll even see an error if you try to index a property that doesn't exist:
type I1 = Person["alve"];
Property
Property 'alve'
'alve' does
does not
not exist
exist on
on type
type 'Person'.
'Person'.
Another example of indexing with an arbitrary type is using number to get the type of an array's
elements. We can combine this with typeof to conveniently capture the element type of an array
literal:
const MyArray = [
{ name: "Alice", age: 15 },
{ name: "Bob", age: 23 },
{ name: "Eve", age: 38 },
];
// Or
type Age2 = Person["age"];
// ^ = type Age2 = number
You can only use types when indexing, meaning you can't use a const to make a variable
reference:
Type
Type 'any'
'any' cannot
cannot bebe used
used as
as an
an index
index type.
type.
'key'
'key' refers
refers to
to aa value,
value, but
but is
is being
being used
used as
as aa type
type here.
here. Did
Did you
you mean
mean
'typeof
'typeof key'?
key'?
However, you can use a type alias for a similar style of refactor:
At the heart of most useful programs, we have to make decisions based on input. JavaScript
programs are no different, but given the fact that values can be easily introspected, those decisions
are also based on the types of the inputs. Conditional types help describe the relation between the
types of inputs and outputs.
interface Animal {
live(): void;
}
interface Dog extends Animal {
woof(): void;
}
Conditional types take a form that looks a little like conditional expressions ( condition ?
trueExpression : falseExpression ) in JavaScript:
When the type on the left of the extends is assignable to the one on the right, then you'll get the
type in the first branch (the "true" branch); otherwise you'll get the type in the latter branch (the
"false" branch).
From the examples above, conditional types might not immediately seem useful - we can tell
ourselves whether or not Dog extends Animal and pick number or string ! But the power
of conditional types comes from using them with generics.
These overloads for createLabel describe a single JavaScript function that makes a choice based on
the types of its inputs. Note a few things:
1. If a library has to make the same sort of choice over and over throughout its API, this
becomes cumbersome.
2. We have to create three overloads: one for each case when we're sure of the type (one for
string and one for number ), and one for the most general case (taking a string |
number ). For every new type createLabel can handle, the number of overloads grows
exponentially.
We can then use that conditional type to simplify out overloads down to a single function with no
overloads.
function createLabel<T extends number | string>(idOrName: T): NameOrId<T>
throw "unimplemented";
}
let a = createLabel("typescript");
// ^ = let a: NameLabel
let b = createLabel(2.8);
// ^ = let b: IdLabel
Often, the checks in a conditional type will provide us with some new information. Just like with
narrowing with type guards can give us a more specific type, the true branch of a conditional type
will further constraint generics by the type we check against.
Type
Type '"message"'
'"message"' cannot
cannot be
be used
used to
to index
index type
type 'T'.
'T'.
In this example, TypeScript errors because T isn't known to have a property called message . We
could constrain T , and TypeScript would no longer complain:
type MessageOf<T extends { message: unknown }> = T["message"];
interface Email {
message: string;
}
interface Dog {
bark(): void;
}
However, what if we wanted MessageOf to take any type, and default to something like never if
a message property isn't available? We can do this by moving the constraint out and introducing
a conditional type:
interface Email {
message: string;
}
interface Dog {
bark(): void;
}
Within the true branch, TypeScript knows that T will have a message property.
As another example, we could also write a type called Flatten that flattens array types to their
element types, but leaves them alone otherwise:
type Flatten<T> = T extends any[] ? T[number] : T;
When Flatten is given an array type, it uses an indexed access with number to fetch out
string[] 's element type. Otherwise, it just returns the type it was given.
We just found ourselves using conditional types to apply constraints and then extract out types.
This ends up being such a common operation that conditional types make it easier.
Conditional types provide us with a way to infer from types we compare against in the true branch
using the infer keyword. For example, we could have inferred the element type in Flatten
instead of fetching it out "manually" with an indexed access type:
Here, we used the infer keyword to declaratively introduce a new generic type variable named
Item instead of specifying how to retrieve the element type of T within the true branch. This
frees us from having to think about how to dig through and probing apart the structure of the types
we're interested in.
We can write some useful helper type aliases using the infer keyword. For example, for simple
cases, we can extract the return type out from function types:
type GetReturnType<Type> = Type extends (...args: never[]) => infer Return
? Return
: never;
When inferring from a type with multiple call signatures (such as the type of an overloaded
function), inferences are made from the last signature (which, presumably, is the most permissive
catch-all case). It is not possible to perform overload resolution based on a list of argument types.
If we plug a union type into ToArray , then the conditional type will be applied to each member of
that union.
type ToArray<Type> = Type extends any ? Type[] : never;
string | number;
and maps over each member type of the union, to what is effectively:
ToArray<string> | ToArray<number>;
string[] | number[];
Typically, distributivity is the desired behavior. To avoid that behavior, you can surround each side of
the extends keyword with square brackets.
When you don't want to repeat yourself, sometimes a type needs to be based on another type.
Mapped types build on the syntax for index signatures, which are used to declare the types of
properties which has not been declared ahead of time:
type OnlyBoolsAndHorses = {
[key: string]: boolean | Horse;
};
A mapped type is a generic type which uses a union created via a keyof to iterate through the keys
of one type to create another:
type OptionsFlags<Type> = {
[Property in keyof Type]: boolean;
};
In this example, OptionFlags will take all the properties from the type Type and change their
values to be a boolean.
type FeatureFlags = {
darkMode: () => void;
newUserProfile: () => void;
};
There are two additional modifiers which can be applied during mapping: readonly and ?
which affect mutability and optionality respectively.
You can remove or add these modifiers by prefixing with - or + . If you don't add a prefix, then +
is assumed.
type LockedAccount = {
readonly id: string;
readonly name: string;
};
type MaybeUser = {
id: string;
name?: string;
age?: number;
};
type MappedTypeWithNewProperties<Type> = {
[Properties in keyof Type as NewKeyType]: Type[Properties]
}
You can leverage features like template literal types to create new property names from prior ones:
type Getters<Type> = {
[Property in keyof Type as `get${Capitalize<string & Property>}`]: ()
};
interface Person {
name: string;
age: number;
location: string;
}
You can filter out keys by producing never via a conditional type:
// Remove the 'kind' property
type RemoveKindField<Type> = {
[Property in keyof Type as Exclude<Property, "kind">]: Type[Property]
};
interface Circle {
kind: "circle";
radius: number;
}
Further Exploration
Mapped types work well with other features in this type manipulation section, for example here is a
mapped type using a conditional type which returns either a true or false depending on
whether an object has the property pii set to the literal true :
type ExtractPII<Type> = {
[Property in keyof Type]: Type[Property] extends { pii: true } ? true :
};
type DBFields = {
id: { format: "incrementing" };
name: { type: string; pii: true };
};
Template literal types build on string literal types, and have the ability to expand into many strings
via unions.
They have the same syntax as template literal strings in JavaScript, but are used in type positions.
When used with concrete literal types, a template literal produces a new string literal type by
concatenating the contents.
When a union is used in the interpolated position, the type is the set of every possible string literal
that could be represented by each union member:
For each interpolated position in the template literal, the unions are cross multiplied:
We generally recommend that people use ahead-of-time generation for large string unions, but this
is useful in smaller cases.
String Unions in Types
The power in template literals comes when defining a new string based off an existing string inside
a type.
For example, a common pattern in JavaScript is to extend an object based on the fields that it
currently has. We'll provide a type definition for a function which adds support for an on function
which lets you know when a value has changed:
Notice that on listens on the event "firstNameChanged" , not just "firstName" , template
literals provide a way to handle this sort of string manipulation inside the type system:
type PropEventSource<Type> = {
on(eventName: `${string & keyof Type}Changed`, callback: (newValue: an
};
With this, we can build something that errors when given the wrong property:
const person = makeWatchedObject({
firstName: "Saoirse",
lastName: "Ronan",
age: 26
});
// It's typo-resistent
person.on("firstName", () => {});
Argument
Argument of
of type
type '"firstName"'
'"firstName"' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type
'"firstNameChanged"
'"firstNameChanged" || "lastNameChanged"
"lastNameChanged" || "ageChanged"'.
"ageChanged"'.
Argument
Argument of
of type
type '"frstNameChanged"'
'"frstNameChanged"' is
is not
not assignable
assignable to
to parameter
parameter of
of
type
type '"firstNameChanged"
'"firstNameChanged" || "lastNameChanged"
"lastNameChanged" || "ageChanged"'.
"ageChanged"'.
Note how the last examples did not re-use the type of the original value. The callback used an any .
Template literal types can infer from substitution positions.
We can make our last example generic to infer from parts of the eventName string to figure out
the associated property.
type PropEventSource<Type> = {
on<Key extends string & keyof Type>
(eventName: `${Key}Changed`, callback: (newValue: Type[Key]) => vo
};
if (newAge < 0) {
console.warn("warning! negative age");
}
})
When a user calls with the string "firstNameChanged' , TypeScript will try to infer the right
type for Key . To do that, it will match Key against the content prior to "Changed" and infer the
string "firstName" . Once TypeScript figures that out, the on method can fetch the type of
firstName on the original object, which is string in this case. Similarly, when called with
"ageChanged" , TypeScript finds the type for the property age which is number .
Inference can be combined in different ways, often to deconstruct strings, and reconstruct them in
different ways.
Uppercase<StringType>
Example
Lowercase<StringType>
Example
Capitalize<StringType>
Example
type LowercaseGreeting = "hello, world";
type Greeting = Capitalize<LowercaseGreeting>;
// ^ = type Greeting = "Hello, world"
Uncapitalize<StringType>
Example
Class Members
Here's the most basic class - an empty one:
class Point {}
This class isn't very useful yet, so let's start adding some members.
Fields
class Point {
x: number;
y: number;
}
As with other locations, the type annotation is optional, but will be an implict any if not specified.
Fields can also have initializers; these will run automatically when the class is instantiated:
class Point {
x = 0;
y = 0;
}
Just like with const , let , and var , the initializer of a class property will be used to infer its
type:
Type
Type 'string'
'string' is
is not
not assignable
assignable to
to type
type 'number'.
'number'.
--strictPropertyInitialization
class BadGreeter {
name: string;
Property
Property 'name'
'name' has
has no
no initializer
initializer and
and is
is not
not definitely
definitely assigned
assigned in
in the
the
constructor.
constructor.
class GoodGreeter {
name: string;
constructor() {
this.name = "hello";
}
}
Note that the field needs to be initialized in the constructor itself. TypeScript does not analyze
methods you invoke from the constructor to detect initializations, because a derived class might
override those methods and fail to initialize the members.
If you intend to definitely initialize a field through means other than the constructor (for example,
maybe an external library is filling in part of your class for you), you can use the definite assignment
assertion operator, ! :
class OKGreeter {
// Not initialized, but no error
name!: string;
}
readonly
Fields may be prefixed with the readonly modifier. This prevents assignments to the field outside
of the constructor.
class Greeter {
readonly name: string = "world";
constructor(otherName?: string) {
if (otherName !== undefined) {
this.name = otherName;
}
}
err() {
this.name = "not ok";
Cannot
Cannot assign
assign to
to 'name'
'name' because
because it
it is
is aa read-only
read-only property.
property.
}
}
const g = new Greeter();
g.name = "also not ok";
Cannot
Cannot assign
assign to
to 'name'
'name' because
because it
it is
is aa read-only
read-only property.
property.
Constructors
Class constructors are very similar to functions. You can add Background Reading:
parameters with type annotations, default values, and overloads: Constructor (MDN)
class Point {
x: number;
y: number;
class Point {
// Overloads
constructor(x: number, y: string);
constructor(s: string);
constructor(xs: any, y?: any) {
// TBD
}
}
There are just a few differences between class constructor signatures and function signatures:
Constructors can't have type parameters - these belong on the outer class declaration, which
we'll learn about later
Constructors can't have return type annotations - the class instance type is always what's
returned
Super Calls
Just as in JavaScript, if you have a base class, you'll need to call super(); in your constructor
body before using any this. members:
class Base {
k = 4;
}
'super'
'super' must
must be
be called
called before
before accessing
accessing 'this'
'this' in
in the
the constructor
constructor of
of aa
derived
derived class.
class.
super();
}
}
Forgetting to call super is an easy mistake to make in JavaScript, but TypeScript will tell you
when it's necessary.
Methods
class Point {
x = 10;
y = 10;
Other than the standard type annotations, TypeScript doesn't add anything else new to methods.
Note that inside a method body, it is still mandatory to access fields and other methods via this. .
An unqualified name in a method body will always refer to something in the enclosing scope:
let x: number = 0;
class C {
x: string = "hello";
m() {
// This is trying to modify 'x' from line 1, not the class property
x = "world";
Type
Type 'string'
'string' is
is not
not assignable
assignable to
to type
type 'number'.
'number'.
}
}
Getters / Setters
class C {
_length = 0;
get length() {
return this._length;
}
set length(value) {
this._length = value;
}
}
Note that a field-backed get/set pair with no extra logic is very rarely useful in JavaScript. It's fine to
expose public fields if you don't need to add additional logic during the get/set operations.
The type of the setter parameter is inferred from the return type of the getter
If the setter parameter has a type annotation, it must match the return type of the getter
It is not possible to have accessors with different types for getting and setting.
If you have a getter without a setter, the field is automatically readonly
Index Signatures
Classes can declare index signatures; these work the same as Index Signatures for other object
types:
class MyClass {
[s: string]: boolean | ((s: string) => boolean);
check(s: string) {
return this[s] as boolean;
}
}
Because the index signature type needs to also capture the types of methods, it's not easy to
usefully use these types. Generally it's better to store indexed data in another place instead of on
the class instance itself.
Class Heritage
Like other langauges with object-oriented features, classes in JavaScript can inherit from base
classes.
implementsClauses
You can use an implements clause to check that a class satisfies a particular interface . An
error will be issued if a class fails to correctly implement it:
interface Pingable {
ping(): void;
}
Class
Class 'Ball'
'Ball' incorrectly
incorrectly implements
implements interface
interface 'Pingable'.
'Pingable'.
Property
Property 'ping'
'ping' is
is missing
missing in
in type
type 'Ball'
'Ball' but
but required
required in
in type
type
'Pingable'.
'Pingable'.
pong() {
console.log("pong!");
}
}
Cautions
It's important to understand that an implements clause is only a check that the class can be
treated as the interface type. It doesn't change the type of the class or its methods at all. A common
source of error is to assume that an implements clause will change the class type - it doesn't!
interface Checkable {
check(name: string): boolean;
}
Parameter
Parameter 's'
's' implicitly
implicitly has
has an
an 'any'
'any' type.
type.
}
}
In this example, we perhaps expected that s 's type would be influenced by the name: string
parameter of check . It is not - implements clauses don't change how the class body is checked
or its type inferred.
Similarly, implementing an interface with an optional property doesn't create that property:
interface A {
x: number;
y?: number;
}
class C implements A {
x = 0;
}
const c = new C();
c.y = 10;
Property
Property 'y'
'y' does
does not
not exist
exist on
on type
type 'C'.
'C'.
extendsClauses
Classes may extend from a base class. A derived class has all the
Background Reading:
properties and methods of its base class, and also define additional
extends keyword (MDN)
members.
class Animal {
move() {
console.log("Moving along!");
}
}
Overriding Methods
A derived class can also override a base class field or property. You can
Background Reading:
use the super. syntax to access base class methods. Note that
super keyword (MDN)
because JavaScript classes are a simple lookup object, there is no
notion of a "super field".
TypeScript enforces that a derived class is always a subtype of its base class.
It's important that a derived class follow its base class contract. Remember that it's very common
(and always legal!) to refer to a derived class instance through a base class reference:
Property
Property 'greet'
'greet' in
in type
type 'Derived'
'Derived' is
is not
not assignable
assignable to
to the
the same
same property
property
in base type
in base
'Base'.
type 'Base'.
Type
Type '(name:
'(name: string)
string) =>
=> void'
void' is
is not
not assignable
assignable to
to type
type '()
'() =>
=> void'.
void'.
console.log(`Hello, ${name.toUpperCase()}`);
}
}
If we compiled this code despite the error, this sample would then crash:
Initialization Order
The order that JavaScript classes initialize can be surprising in some cases. Let's consider this code:
class Base {
name = "base";
constructor() {
console.log("My name is " + this.name);
}
}
This means that the base class constructor saw its own value for name during its own constructor,
because the derived class field initializations hadn't run yet.
Note: If you don't plan to inherit from built-in types like Array , Error , Map , etc., you may skip this
section
In ES2015, constructors which return an object implicitly substitute the value of this for any
callers of super(...) . It is necessary for generated constructor code to capture any potential
return value of super(...) and replace it with this .
As a result, subclassing Error , Array , and others may no longer work as expected. This is due
to the fact that constructor functions for Error , Array , and the like use ECMAScript 6's
new.target to adjust the prototype chain; however, there is no way to ensure a value for
new.target when invoking a constructor in ECMAScript 5. Other downlevel compilers generally
have the same limitation by default.
instanceof will be broken between instances of the subclass and their instances, so (new
MsgError()) instanceof MsgError will return false .
As a recommendation, you can manually adjust the prototype immediately after any super(...)
calls.
sayHello() {
return "hello " + this.message;
}
}
However, any subclass of MsgError will have to manually set the prototype as well. For runtimes
that don't support Object.setPrototypeOf, you may instead be able to use __proto__.
Unfortunately, these workarounds will not work on Internet Explorer 10 and prior. One can
manually copy methods from the prototype onto the instance itself (i.e. MsgError.prototype
onto this ), but the prototype chain itself cannot be fixed.
Member Visibility
You can use TypeScript to control whether certain methods or properties are visible to code outside
the class.
public
The default visibility of class members is public . A public member can be accessed by
anywhere:
class Greeter {
public greet() {
console.log("hi!");
}
}
const g = new Greeter();
g.greet();
Because public is already the default visibility modifier, you don't ever need to write it on a class
member, but might choose to do so for style/readability reasons.
protected
protected members are only visible to subclasses of the class they're declared in.
class Greeter {
public greet() {
console.log("Hello, " + this.getName());
}
protected getName() {
return "hi";
}
}
Property
Property 'getName'
'getName' is
is protected
protected and
and only
only accessible
accessible within
within class
class 'Greeter'
'Greeter'
and its subclasses.
and its subclasses.
Derived classes need to follow their base class contracts, but may choose to expose a subtype of
base class with more capabilities. This includes making protected members public :
class Base {
protected m = 10;
}
class Derived extends Base {
// No modifier, so default is 'public'
m = 15;
}
const d = new Derived();
console.log(d.m); // OK
Note that Derived was already able to freely read and write m , so this doesn't meaningfully alter
the "security" of this situation. The main thing to note here is that in the derived class, we need to be
careful to repeat the protected modifier if this exposure isn't intentional.
Different OOP languages disagree about whether it's legal to access a protected member
through a base class reference:
class Base {
protected x: number = 1;
}
class Derived1 extends Base {
protected x: number = 5;
}
class Derived2 extends Base {
f1(other: Derived2) {
other.x = 10;
}
f2(other: Base) {
other.x = 10;
Property
Property 'x'
'x' is
is protected
protected and
and only
only accessible
accessible through
through an
an instance
instance of
of class
class
'Derived2'.
'Derived2'.
}
}
Java, for example, considers this to be legal. On the other hand, C# and C++ chose that this code
should be illegal.
TypeScript sides with C# and C++ here, because accessing x in Derived2 should only be legal
from Derived2 's subclasses, and Derived1 isn't one of them. Moreover, if accessing x
through a Derived2 reference is illegal (which it certainly should be!), then accessing it through a
base class reference should never improve the situation.
See also Why Can’t I Access A Protected Member From A Derived Class? which explains more of
C#'s reasoning.
private
private is like protected , but doesn't allow access to the member even from subclasses:
class Base {
private x = 0;
}
const b = new Base();
// Can't access from outside the class
console.log(b.x);
Property
Property 'x'
'x' is
is private
private and
and only
only accessible
accessible within
within class
class 'Base'.
'Base'.
Property
Property 'x'
'x' is
is private
private and
and only
only accessible
accessible within
within class
class 'Base'.
'Base'.
}
}
Because private members aren't visible to derived classes, a derived class can't increase its
visibility:
class Base {
private x = 0;
}
class Derived extends Base {
Class
Class 'Derived'
'Derived' incorrectly
incorrectly extends
extends base
base class
class 'Base'.
'Base'.
Property
Property 'x'
'x' is
is private
private in
in type
type 'Base'
'Base' but
but not
not in
in type
type 'Derived'.
'Derived'.
x = 1;
}
Different OOP languages disagree about whether different instances of the same class may access
each others' private members. While languages like Java, C#, C++, Swift, and PHP allow this,
Ruby does not.
TypeScript does allow cross-instance private access:
class A {
private x = 10;
public sameAs(other: A) {
// No error
return other.x === this.x;
}
}
Caveats
Like other aspects of TypeScript's type system, private and protected are only enforced
during type checking. This means that JavaScript runtime constructs like in or simple property
lookup can still access a private or protected member:
class MySafe {
private secretKey = 12345;
}
// In a JavaScript file...
const s = new MySafe();
// Will print 12345
console.log(s.secretKey);
If you need to protect values in your class from malicious actors, you should use mechanisms that
offer hard runtime privacy, such as closures, weak maps, or private fields.
Static Members
Classes may have static members. These members aren't
Background Reading:
associated with a particular instance of the class. They can be
Static Members (MDN)
accessed through the class constructor object itself:
class MyClass {
static x = 0;
static printX() {
console.log(MyClass.x);
}
}
console.log(MyClass.x);
MyClass.printX();
Static members can also use the same public , protected , and private visibility modifiers:
class MyClass {
private static x = 0;
}
console.log(MyClass.x);
Property
Property 'x'
'x' is
is private
private and
and only
only accessible
accessible within
within class
class 'MyClass'.
'MyClass'.
class Base {
static getGreeting() {
return "Hello world";
}
}
class Derived extends Base {
myGreeting = Derived.getGreeting();
}
It's generally not safe/possible to overwrite properties from the Function prototype. Because
classes are themselves functions that can be invoked with new , certain static names can't be
used. Function properties like name , length , and call aren't valid to define as static
members:
class S {
static name = "S!";
Static
Static property
property 'name'
'name' conflicts
conflicts with
with built-in
built-in property
property 'Function.name'
'Function.name' of
of
constructor
constructor
function
function
'S'.'S'.
TypeScript (and JavaScript) don't have a construct called static class the same way C# and
Java do.
Those constructs only exist because those languages force all data and functions to be inside a
class; because that restriction doesn't exist in TypeScript, there's no need for them. A class with only
a single instance is typically just represented as a normal object in JavaScript/TypeScript.
For example, we don't need a "static class" syntax in TypeScript because a regular object (or even
top-level function) will do the job just as well:
// Preferred (alternative 1)
function doSomething() {}
// Preferred (alternative 2)
const MyHelperObject = {
dosomething() {},
};
Generic Classes
Classes, much like interfaces, can be generic. When a generic class is instantiated with new , its type
parameters are inferred the same way as in a function call:
class Box<Type> {
contents: Type;
constructor(value: Type) {
this.contents = value;
}
}
Classes can use generic constraints and defaults the same way as interfaces.
class Box<Type> {
static defaultValue: Type;
Static
Static members
members cannot
cannot reference
reference class
class type
type parameters.
parameters.
Remember that types are always fully erased! At runtime, there's only one Box.defaultValue
property slot. This means that setting Box<string>.defaultValue (if that were possible)
would also change Box<number>.defaultValue - not good. The static members of a
generic class can never refer to the class's type parameters.
Long story short, by default, the value of this inside a function depends on how the function was
called. In this example, because the function was called through the obj reference, its value of
this was obj rather than the class instance.
This is rarely what you want to happen! TypeScript provides some ways to mitigate or prevent this
kind of error.
Arrow Functions
If you have a function that will often be called in a way that loses its
Background Reading:
this context, it can make sense to use an arrow function property
Arrow functions (MDN)
instead of a method definition:
class MyClass {
name = "MyClass";
getName = () => {
return this.name;
};
}
const c = new MyClass();
const g = c.getName;
// Prints "MyClass" instead of crashing
console.log(g());
This has some trade-offs:
The this value is guaranteed to be correct at runtime, even for code not checked with
TypeScript
This will use more memory, because each class instance will have its own copy of each function
defined this way
You can't use super.getName in a derived class, because there's no entry in the prototype
chain to fetch the base class method from
thisparameters
In a method or function definition, an initial parameter named this has special meaning in
TypeScript. These parameters are erased during compilation:
// JavaScript output
function fn(x) {
/* ... */
}
TypeScript checks that calling a function with a this parameter is done so with a correct context.
Instead of using an arrow function, we can add a this parameter to method definitions to
statically enforce that the method is called correctly:
class MyClass {
name = "MyClass";
getName(this: MyClass) {
return this.name;
}
}
const c = new MyClass();
// OK
c.getName();
The
The 'this'
'this' context
context of
of type
type 'void'
'void' is
is not
not assignable
assignable to
to method's
method's 'this'
'this' of
of
typetype
'MyClass'.
'MyClass'.
This method takes the opposite trade-offs of the arrow function approach:
JavaScript callers might still use the class method incorrectly without realizing it
Only one function per class definition gets allocated, rather than one per class instance
Base method definitions can still be called via super.
thisTypes
In classes, a special type called this refers dynamically to the type of the current class. Let's see
how this is useful:
class Box {
contents: string = "";
set(value: string) {
// ^ = (method) Box.set(value: string): this
this.contents = value;
return this;
}
}
Here, TypeScript inferred the return type of set to be this , rather than Box . Now let's make a
subclass of Box :
class Box {
content: string = "";
sameAs(other: this) {
return other.content === this.content;
}
}
This is different from writing other: Box -- if you have a derived class, its sameAs method will
now only accept other instances of that same derived class:
class Box {
content: string = "";
sameAs(other: this) {
return other.content === this.content;
}
}
Argument
Argument of
of type
type 'Box'
'Box' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type
'DerivedBox'.
'DerivedBox'.
Property
Property 'otherContent'
'otherContent' is
is missing
missing in
in type
type 'Box'
'Box' but
but required
required in
in type
type
'DerivedBox'.
'DerivedBox'.
You can use this is Type in the return position for methods in classes and interfaces. When
mixed with a type narrowing (e.g. if statements) the type of the target object would be narrowed
to the specified Type .
class FileSystemObject {
isFile(): this is FileRep {
return this instanceof FileRep;
}
isDirectory(): this is Directory {
return this instanceof Directory;
}
isNetworked(): this is Networked & this {
return this.networked;
}
constructor(public path: string, private networked: boolean) {}
}
interface Networked {
host: string;
}
if (fso.isFile()) {
fso.content;
// ^ = const fso: FileRep
} else if (fso.isDirectory()) {
fso.children;
// ^ = const fso: Directory
} else if (fso.isNetworked()) {
fso.host;
// ^ = const fso: Networked & FileSystemObject
}
A common use-case for a this-based type guard is to allow for lazy validation of a particular field.
For example, this case removes an undefined from the value held inside box when hasValue
has been verified to be true:
class Box<T> {
value?: T;
box.value;
// ^ = (property) Box<unknown>.value?: unknown
if (box.hasValue()) {
box.value;
// ^ = (property) value: unknown
Parameter Properties
TypeScript offers special syntax for turning a constructor parameter into a class property with the
same name and value. These are called parameter properties and are created by prefixing a
constructor argument with one of the visibility modifiers public , private , protected , or
readonly . The resulting field gets those modifier(s):
class Params {
constructor(
public readonly x: number,
protected y: number,
private z: number
) {
// No body necessary
}
}
const a = new Params(1, 2, 3);
console.log(a.x);
// ^ = (property) Params.x: number
console.log(a.z);
Property
Property 'z'
'z' is
is private
private and
and only
only accessible
accessible within
within class
class 'Params'.
'Params'.
Class Expressions
Class expressions are very similar to class declarations. The only
Background Reading:
real difference is that class expressions don't need a name, though
Class expressions (MDN)
we can refer to them via whatever identifier they ended up bound
to:
The role of abstract classes is to serve as a base class for subclasses which do implement all the
abstract members. When a class doesn't have any abstract members, it is said to be concrete.
printName() {
console.log("Hello, " + this.getName());
}
}
Cannot
Cannot create
create an
an instance
instance of
of an
an abstract
abstract class.
class.
We can't instantiate Base with new because it's abstract. Instead, we need to make a derived
class and implement the abstract members:
Notice that if we forget to implement the base class's abstract members, we'll get an error:
class Derived extends Base {
Non-abstract
Non-abstract class
class 'Derived'
'Derived' does
does not
not implement
implement inherited
inherited abstract
abstract member
member
'getName'
'getName'
from class
from 'Base'.
class 'Base'.
// forgot to do anything
}
Sometimes you want to accept some class constructor function that produces an instance of a class
which derives from some abstract class.
Cannot
Cannot create
create an
an instance
instance of
of an
an abstract
abstract class.
class.
instance.printName();
}
TypeScript is correctly telling you that you're trying to instantiate an abstract class. After all, given
the definition of greet , it's perfectly legal to write this code, which would end up constructing an
abstract class:
// Bad!
greet(Base);
Instead, you want to write a function that accepts something with a construct signature:
function greet(ctor: new () => Base) {
const instance = new ctor();
instance.printName();
}
greet(Derived);
greet(Base);
Argument
Argument of
of type
type 'typeof
'typeof Base'
Base' is
is not
not assignable
assignable to
to parameter
parameter of
of type
type 'new
'new
() =>()Base'.
=> Base'.
Cannot
Cannot assign
assign an
an abstract
abstract constructor
constructor type
type to
to aa non-abstract
non-abstract constructor
constructor
type. type.
Now TypeScript correctly tells you about which class constructor functions can be invoked -
Derived can because it's concrete, but Base cannot.
For example, these two classes can be used in place of each other because they're identical:
class Point1 {
x = 0;
y = 0;
}
class Point2 {
x = 0;
y = 0;
}
// OK
const p: Point1 = new Point2();
Similarly, subtype relationships between classes exist even if there's no explicit inheritance:
class Person {
name: string;
age: number;
}
class Employee {
name: string;
age: number;
salary: number;
}
// OK
const p: Person = new Employee();
This sounds straightforward, but there are a few cases that seem stranger than others.
Empty classes have no members. In a structural type system, a type with no members is generally a
supertype of anything else. So if you write an empty class (don't!), anything can be used in place of
it:
class Empty {}
// All OK!
fn(window);
fn({});
fn(fn);
Modules
JavaScript has a long history of different ways to handle modularizing code. TypeScript having been
around since 2012, has implemented support for a lot of these formats, but over time the
community and the JavaScript specification has converged on a format called ES Modules (or ES6
modules). You might know it as the import / export syntax.
ES Modules was added to the JavaScript spec in 2015, and by 2020 had broad support in most web
browsers and JavaScript runtimes.
For focus, the handbook will cover both ES Modules and it's popular pre-cursor CommonJS
module.exports = syntax, and you can find information about the other module patterns in
the reference section under Modules.
Conversely, a file without any top-level import or export declarations is treated as a script whose
contents are available in the global scope (and therefore to modules as well).
Modules are executed within their own scope, not in the global scope. This means that variables,
functions, classes, etc. declared in a module are not visible outside the module unless they are
explicitly exported using one of the export forms. Conversely, to consume a variable, function, class,
interface, etc. exported from a different module, it has to be imported using one of the import
forms.
Non-modules
Before we start, it's important to understand what TypeScript considers a module. The JavaScript
specification declares that any JavaScript files without an export or top-level await should be
considered a script and not a module.
Inside a script file variables and types are declared to be in the shared global scope, and it's
assumed that you'll either use the --outFile compiler option to join multiple input files into one
output file, or use multiple <script> tags in your HTML to load these files (in the correct order!).
If you have a file that doesn't currently have any import s or export s, but you want to be
treated as a module, add the line:
export {};
which will change the file be a module exporting nothing. This syntax works regardless of your
module target.
Modules in TypeScript
There are three main things to consider when writing module-
Additional Reading:
based code in TypeScript:
Impatient JS (Modules)
MDN: JavaScript Modules
Syntax: What syntax do I want to use to import and export
things?
Module Resolution: What is the relationship between module names (or paths) and files on
disk?
Module Output Target: What should my emitted JavaScript module look like?
ES Module Syntax
// @filename: hello.ts
export default function helloWorld() {
console.log("Hello, world!");
}
In addition to the default export, you can have more than one export of variables and functions via
the export by omitting default :
// @filename: maths.ts
export var pi = 3.14;
export let squareTwo = 1.41;
export const phi = 1.61;
console.log(pi);
const absPhi = absolute(phi);
// ^ = const absPhi: number
console.log(Ï€);
// ^ = (alias) var π: number
// import π
You can mix and match the above syntax into a single import :
// @filename: maths.ts
export const pi = 3.14;
export default class RandomNumberGenerator {}
// @filename: app.ts
import RNGen, { pi as π } from "./maths.js";
RNGen;
// ^ = (alias) class RNGen
// import RNGen
console.log(Ï€);
// ^ = (alias) const π: 3.14
// import π
You can take all of the exported objects and put them into a single namespace using * as name :
// @filename: app.ts
import * as math from "./maths.js";
console.log(math.pi);
const positivePhi = math.absolute(math.phi);
// ^ = const positivePhi: number
You can import a file and not include any variables into your current module via import
"./file" :
// @filename: app.ts
import "./maths.js";
console.log("3.14");
In this case, the import does nothing. However, all of the code in maths.ts was evaluated,
which could trigger side-effects which affect other objects.
// @filename: animal.ts
export type Cat = { breed: string; yearOfBirth: number };
// @filename: app.ts
import { Cat, Dog } from "./animal.js";
type Animals = Cat | Dog;
TypeScript has extended the import syntax with import type which is an import which can
only import types.
// @filename: animal.ts
export type Cat = { breed: string; yearOfBirth: number };
'createCatName'
'createCatName' cannot
cannot be
be used
used as
as aa value
value because
because it
it was
was imported
imported using
using
'import
'import type'.
type'.
// @filename: valid.ts
import type { Cat, Dog } from "./animal.js";
export type Animals = Cat | Dog;
// @filename: app.ts
import type { createCatName } from "./animal.js";
const name = createCatName();
This syntax allows a non-TypeScript transpiler like Babel, swc or esbuild to know what imports can
be safely removed.
TypeScript has ES Module syntax which directly correlates to a CommonJS and AMD require .
Imports using ES Module are for most cases the same as the require from those environments,
but this syntax ensures you have a 1 to 1 match in your TypeScript file with the CommonJS output:
import fs = require("fs");
const code = fs.readFileSync("hello.ts", "utf8");
You can learn more about this syntax in the modules reference page.
CommonJS Syntax
CommonJS is the format which most modules on npm are delivered in. Even if you are writing
using the ES Modules syntax above, having an brief understanding of how CommonJS syntax
works will help you debug easier.
Exporting
Identifiers are exported via setting the exports property on a global called module .
module.exports = {
pi: 3.14,
squareTwo: 1.41,
phi: 1.61,
absolute,
};
There is a mis-match in features between CommonJS and ES Module because ES Modules only
support having the default export as a object, and never as a function. TypeScript has a compiler
flag to reduce the friction between the two different sets of constraints with esModuleInterop.
TypeScript includes two resolution strategies: Classic and Node. Classic, the default when the
compiler flag module is not commonjs , is included for backwards compatibility. The Node
strategy replicates how Node.js works in CommonJS mode, with additional checks for .ts and
.d.ts .
There are many TSConfig flags which influence the module strategy within TypeScript:
moduleResolution, baseUrl, paths, rootDirs.
For the full details on how these strategies work, you can consult the Module Resolution.
target which determines which JS features are downleveled (converted to run in older
JavaScript runtimes) and which are left intact
module which determines what code is used for modules to interact with each other
Which target you use is determined by the features available in the JavaScript runtime you
expect to run the TypeScript code in. That could be: the oldest web browser you support, the lowest
version of Node.js you expect to run on or could come from unique constraints from your runtime -
like Electron for example.
All communication between modules happens via a module loader, the compiler flag module
determines which one is used. At runtime the module loader is responsible for locating and
executing all dependencies of a module before executing it.
For example, here is a TypeScript file using ES Modules syntax, showcasing a few different options
for module:
ES2020
CommonJS
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.twoPi = void 0;
const constants_js_1 = require("./constants.js");
exports.twoPi = constants_js_1.valueOfPi * 2;
UMD
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object")
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "./constants.js"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.twoPi = void 0;
const constants_js_1 = require("./constants.js");
exports.twoPi = constants_js_1.valueOfPi * 2;
});
You can see all of the available options and what their emitted JavaScript code looks like in the
TSConfig Reference for module.
TypeScript namespaces
TypeScript has it's own module format called namespaces which pre-dates the ES Modules
standard. This syntax has a lot of useful features for creating complex definition files, and still sees
active use in DefinitelyTyped. While not deprecated, the majority of the features in namespaces exist
in ES Modules and we recommend you use that to align with JavaScript's direction. You can learn
more about namespaces in the namespaces reference page.