Tsadvanced
Tsadvanced
T
A D V A N C E D
THIS BOOK
This book is written (typed)
by Ari, who hails from the
South and has keen
interests in Computer
Science, Biology, and Tamil
Literature. Occasionally, he
updates his website, where
you can reach out to him.
https://arihara-
sudhan.github.io
Kindly go through the
following book before
reading this.book
TYPESCRIPT
TypeScript is described as a
“superset of JavaScript” or
“JavaScript with types.” It is
four things:
>> Programming Language
A language that includes all
the JavaScript syntax, plus
new TypeScript syntax for
types.
>> Type Checker
A program that takes in a
set of files written in
JavaScript and/or
TypeScript, develops an
understanding of all the
constructs created, and lets
the developer know if it
thinks anything is set up
incorrectly.
>> Transpiler
A program that runs the
type checker, reports any
issues, then outputs the
equivalent JavaScript code.
>> Language service
A program that uses the
type checker to tell editors
how to provide helpful
utilities to developers.
EXAMPLES
When we code the following
in TypeScript (I am using
TypeScript playground),
It (The Language Service)
will come to us with a
complaint. It would use its
knowledge that the length
property of a string is a
number; not a function. We
can also hover on the
highlight and know what it’s
all about. TypeScript can
give us confidence that
changes in one area of code
won’t break other areas of
code that use it.
For an instance, if we
change the number of
required parameters for a
function, TypeScript will let
us know if we forget to
update a place that calls
the function.
We can hover on it and
ONCE INFERRED!
INFERRED!
Once the type of the data is
inferred, it can’t be
changed later.
UNIONS
There maybe some cases
like the following.
What could be the type of
nameOfHim when we hover
on it? What could be the
type inferred? It’s Union!
NARROWING
Narrowing is when
TypeScript infers from our
code that a value is of a
more specific type than
what it was defined,
declared, or previously
inferred as.
inferred as literal.
If we hover on the variable,
Literal is a type itself if used
in the place where type has
to be used.
STRICT NULL CHECKING
In TS, we can enable or
TYPE ALIASES
If we have to use longer
union types that are
inconvenient to type out
repeatedly, we can use
type aliases. Actually, it’s
not only for Unions. We can
use it to alias any types.
shown below.
This is one of the places
where we can use type
aliasing.
STRUCTURAL TYPING
Structural typing means
that two objects are
considered compatible as
long as they have the same
structure, regardless of
what they are explicitly
typed as. TypeScript checks
whether an object has the
as shown below:
OPTIONAL PROPERTIES
We can include a ? before
the : in a type property’s
type annotation to indicate
that it’s an optional
property.
If we want to perform or
simulate something called
Function Overloading in
JavaScript,
We may need to check
arguments.length and
proceed accordingly. But,
here in TypeScript, we can
make it easy!
TypeScript simplifies
function overloading by
allowing us to define
multiple function signatures
for the same function
name. This enables us to
specify different parameter
types and return types.
ARRAYS
In JS, what we call as Array
I hope there is no
perplexity!
We have to be careful with
the paranthesis.
Array types can be evolved.
Yes! Every evolving array
members! It is through
using any.
We can define types for
multidimensional arrays.
TypeScript understands
typical index-based access
for retrieving members of
an array to give back an
element of that array’s
type.
If we want to convert an
array into a read-only tuple,
we can do that with const
assertion.
INTERFACES
We can make our own
types. It’s not like aliasing.
A type alias is a way to
create a new name for any
type, including primitives,
unions, intersections, and
other types like functions
and tuples. An interface is a
way to define the shape of
an object, focusing
primarily on object
structure. It is more flexible
when working with object-
oriented concepts and
supports declaration
merging. Interfaces can
“merge” together to be
augmented and can be
used to type check the
structure of class
declarations while type
aliases cannot.
We’ll see these later. So,
let’s define an interface
now.
As in type aliases, we can’t
assign a wrong typed value.
We can also have optional
properties as well.
Besides, we can make a
property read-only using
readonly modifier.
It’s very common in
JavaScript for object
members to be functions.
TypeScript therefore allows
declaring interface
members as being the
function types.
Both parameters can be
optional.
A call signature in an
interface is a way to define
how a function should be
structured—what
parameters it takes and
what type it returns.
Interface defines a
structure or contract for an
object. Meanwhile, A Call
Signature defines how a
function should be called,
including its parameters
and return type.
Index signatures allow us to
define the types of keys
and their corresponding
values in an object, without
knowing the exact names of
the keys upfront.
Remember! It is also
parameter types,
TypeScript creates
overloads.
CLASSES
Classes are blueprints for
creating objects. They
encapsulate data
(properties) and behavior
(methods) into a single
entity, enabling Object-
Oriented Programming
principles.
Class properties are
variables that belong to a
class. They define the state
of an object created from
the class.
TypeScript checks that class
properties are properly
initialized before they are
used. If a property is not
assigned a value in the
constructor, TypeScript will
raise an error.
Function properties are
methods defined within a
class that can be called on
instances of that class.
They define behaviours
another type.
The typeof operator is used
to get the type of a variable
or object at compile time. It
can also be used to create
types based on existing
variable types.
GENERICS
Generics are used to define
a component like a
function, class, or interface
that can work with multiple
types rather than a single
one. We can think of it as a
way to pass types as
parameters to components.
This increases code
flexibility and reusability.
Look at the following
Generic?
Let’s extend a generic
class!
It becomes interesting
when we implement a
generic interface.
Methods in a class can also
be generic.
The Promise.reject(err)
function, like the
Promise.resolve method,
always returns a rejected
promise. The promise
rejection value is derived
from the error parameter,
and its type is any. The
async and await provide a
cleaner, more readable way
to work with promises in
TypeScript. An async
function always returns a
promise, and you use await
to pause the execution until
the promise is resolved or
rejected.
Promises can be typed for
better type safety!
NANDRI